Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Passing form data to PHP

  1. May 10, 2007 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I am working on a simple example of trying to pass form data to my php program.

    I am not sure how to pass the variable that holds the data the user enters.

    Here's what I've tried. (note, it only looks right in Firefox, but that's what we're supposed to be writing it for).

    http://www.pic.ucla.edu/~jctaylor/callhello3.htm

    and this is my php script:

    #!/usr/local/bin/php
    <?php
    echo $_GET['hello'];
    ?>

    Thanks for any help!
    (oh, and I am using jquery libraries - forgot to mention)
     
    Last edited: May 10, 2007
  2. jcsd
  3. May 10, 2007 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

  4. May 10, 2007 #3
    the problem is this line where you're trying to pass the contents of the keywords text field to your script
    Code (Text):
    $("//div#content").load("hello.php?hello=keywords");
    The way it's written, you're telling hello.php to run with a variable called hello set to "keywords".

    try something like
    Code (Text):
    $("//div#content").load("hello.php?hello="+this.keywords);
    which would (should) append the query string with the value from the keywords element.

    Oh, and the script will break if you enter more than one word. Working around that I'll leave as an exercise for you.
     
  5. May 10, 2007 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    oh, thanks!! I appreciate the help!! I'll give it a try!:cool:
     
  6. May 11, 2007 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    hmmm.. still haven't got it sorted out. It says "undefined".
     
  7. May 11, 2007 #6

    -Job-

    User Avatar
    Science Advisor

    Since your input object only has a name property (and no ID)
    Code (Text):

    <INPUT name=keywords>
     
    You can use the following to get its value:
    Code (Text):

    document.getElementsByName('keywords')[0].value
     
    So in your code you'd have:
    Code (Text):

    <SCRIPT type=text/javascript>
          $(document).ready(
            function(){
              $("//p#b1/a").click(
                function(){
                  $("//div#content").empty();
                  $("//div#content").load("hello.php?hello=" + document.getElementsByName('keywords')[0].value);

                }
              )
            }
          )
        </SCRIPT>
     
    If you give your input control an ID:
    Code (Text):

    <INPUT name=keywords id=keywords>
     
    You can use the following to get its value:
    Code (Text):

    document.getElementById('keywords').value;
     
    Or you can use jquery:
    Code (Text):

    $("input#keywords").val()
     
    And you'd have, after giving your control an ID:
    Code (Text):

    <SCRIPT type=text/javascript>
          $(document).ready(
            function(){
              $("//p#b1/a").click(
                function(){
                  $("//div#content").empty();
                  $("//div#content").load("hello.php?hello=" + $("input#keywords").val());

                }
              )
            }
          )
        </SCRIPT>
     
     
    Last edited: May 11, 2007
  8. May 11, 2007 #7

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?