How do I pass form data to my PHP program?

  • Context: PHP 
  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Data Form Php
Click For Summary

Discussion Overview

The discussion revolves around passing form data to a PHP program using JavaScript and jQuery. Participants explore methods to retrieve user input from a form and send it to a PHP script via a URL query string.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes their attempt to pass form data to a PHP script and expresses uncertainty about how to correctly reference the input variable.
  • Another participant explains that the $_GET variable refers to the URL query string and provides an example of how to format the URL correctly.
  • A different participant points out an issue with the original JavaScript code, suggesting a modification to correctly append the value from the keywords input field to the query string.
  • Further suggestions are made regarding how to retrieve the value of the input field, including using document.getElementsByName and jQuery methods.
  • One participant reports success after implementing the suggested changes, indicating that the input field now works as intended.

Areas of Agreement / Disagreement

Participants generally agree on the methods to retrieve and pass form data, but there are variations in the specific implementations suggested. The discussion remains focused on technical solutions without a clear consensus on a single best approach.

Contextual Notes

Some limitations include the potential for issues when entering multiple words in the input field, as noted by one participant. Additionally, the discussion assumes familiarity with JavaScript and jQuery, which may not be universal among all readers.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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 by a moderator:
Technology news on Phys.org
$_GET referes to the URL query string. A URL's query string is anything that comes after a '?' character.

For example,

http://www.pic.ucla.edu/~jctaylor/callhello3.htm?hello=goodbye

- Warren
 
Last edited by a moderator:
the problem is this line where you're trying to pass the contents of the keywords text field to your script
Code:
$("//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:
$("//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.
 
oh, thanks! I appreciate the help! I'll give it a try!:cool:
 
hmmm.. still haven't got it sorted out. It says "undefined".
 
Since your input object only has a name property (and no ID)
Code:
<INPUT name=keywords>

You can use the following to get its value:
Code:
document.getElementsByName('keywords')[0].value

So in your code you'd have:
Code:
<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:
<INPUT name=keywords id=keywords>

You can use the following to get its value:
Code:
document.getElementById('keywords').value;

Or you can use jquery:
Code:
$("input#keywords").val()

And you'd have, after giving your control an ID:
Code:
<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:
oh, OHHH My GOODNESS! It's working! Oh, wow - cool! per your suggestion, I gave the INPUT an ID property and used jquery to get the value.

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

Thanks so much, Job!:smile: :smile: :smile:
 
Last edited by a moderator:

Similar threads

Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
2
Views
2K
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
1
Views
4K
Replies
33
Views
3K
Replies
19
Views
4K