How do I pass form data to my PHP program?

In summary, the conversation discusses passing form data to a php program and the issues encountered while doing so. Various solutions are suggested, including using the URL query string and using jquery to get the value of an input element. Eventually, a working solution is found.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
$_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:
  • #3
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.
 
  • #4
oh, thanks! I appreciate the help! I'll give it a try!:cool:
 
  • #5
hmmm.. still haven't got it sorted out. It says "undefined".
 
  • #6
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:
  • #7
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:

1. How do I pass form data to PHP?

To pass form data to PHP, you can use the method attribute in the <form> tag. Set the method to post or get depending on your needs. Then, in your PHP file, use the $_POST or $_GET superglobal arrays to retrieve the form data.

2. What is the difference between POST and GET methods when passing form data?

The main difference between POST and GET methods is the way data is transferred. POST sends the data in the HTTP request body, while GET appends the data to the URL. This means that GET has a limit on the amount of data that can be sent, while POST does not. Additionally, POST is more secure as the data is not visible in the URL.

3. How can I validate form data in PHP?

To validate form data in PHP, you can use built-in functions like filter_var() or preg_match(). These functions allow you to check if the data meets certain criteria, such as being a valid email or containing only letters and numbers. You can also use conditional statements and regular expressions to validate form data.

4. Can I pass multiple values with the same name from a form?

Yes, you can pass multiple values with the same name from a form by using square brackets [] after the name attribute in the form element. For example, <input type="checkbox" name="hobbies[]" value="reading">. In the PHP file, you can access these multiple values using the $_POST or $_GET superglobal arrays and the name of the form element.

5. How can I prevent malicious users from submitting data to my PHP form?

To prevent malicious users from submitting data to your PHP form, you can use server-side validation by checking the data for any malicious code or unwanted characters. You can also use client-side validation using JavaScript to ensure that the data entered meets certain criteria before it is submitted. Additionally, you can implement security measures like CAPTCHA or CSRF tokens to prevent automated spam submissions.

Similar threads

  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
2
Views
9K
Replies
2
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
1
Views
827
  • Programming and Computer Science
Replies
4
Views
3K
Replies
1
Views
3K
Back
Top