Getting Data from Forms
Moderator: General Moderators
Getting Data from Forms
Hello,
First, I just want you to know I'm a PHP newbie. I know Java so the classes make all kinds of sense, but I'm trying to do some very simple things right now.
What I like'd to do is take a form that involves checkboxes and get data from that to make queries into MySQL (specifically, delete rows based on the data from the form).
So, I was doing this last night and was failing, but that's to be expected as it was late.
The data that fills the form is from a MySQL query that I think is right. Here is what the form looks like when viewing the source:
<input type=checkbox name="Test">Test</input>
I know it does output something (I tried it with a formmail script) and it sends something like:
Test: On
My form action and submit button looks like:
<form action="deletelinks.php" method="post">
<input type=submit name=deletelinks value="Delete">
And my problems come with the "deletelinks.php" file. What should be in this to get the form data into PHP variables.
Right now the "deletelinks.php" page is basically blank. But I was trying stuff like:
$formsubmittedata = $_REQUEST['name'];
I'm sorry if this is very elementary, I've done very little work with PHP, but so far I really like it.
If you're wondering, this is my entire deletelinks.php file (print call was for testing purposes):
<?
$formVariableName = $_REQUEST['name'];
$recipient = $formVariableName;
print($recipient);
?>
Thank you very much for any help in advance,
Eric
First, I just want you to know I'm a PHP newbie. I know Java so the classes make all kinds of sense, but I'm trying to do some very simple things right now.
What I like'd to do is take a form that involves checkboxes and get data from that to make queries into MySQL (specifically, delete rows based on the data from the form).
So, I was doing this last night and was failing, but that's to be expected as it was late.
The data that fills the form is from a MySQL query that I think is right. Here is what the form looks like when viewing the source:
<input type=checkbox name="Test">Test</input>
I know it does output something (I tried it with a formmail script) and it sends something like:
Test: On
My form action and submit button looks like:
<form action="deletelinks.php" method="post">
<input type=submit name=deletelinks value="Delete">
And my problems come with the "deletelinks.php" file. What should be in this to get the form data into PHP variables.
Right now the "deletelinks.php" page is basically blank. But I was trying stuff like:
$formsubmittedata = $_REQUEST['name'];
I'm sorry if this is very elementary, I've done very little work with PHP, but so far I really like it.
If you're wondering, this is my entire deletelinks.php file (print call was for testing purposes):
<?
$formVariableName = $_REQUEST['name'];
$recipient = $formVariableName;
print($recipient);
?>
Thank you very much for any help in advance,
Eric
It's often useful to try this when debugging a form submission script:
Try a forum search for more info on using checkboxes.
Code: Select all
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Last edited by McGruff on Wed Aug 10, 2005 5:31 am, edited 1 time in total.
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
You still need to give a value to the input checkbox.
So your html might look like:
<input type="checkbox" name="mycheckbox" value="on">
Then, if there is a checkmark in that box, on the action page $_POST["mycheckbox"] will contain "on".
If there is no checkmark, then $_POST["mycheckbox"] will be unset.
So your html might look like:
<input type="checkbox" name="mycheckbox" value="on">
Then, if there is a checkmark in that box, on the action page $_POST["mycheckbox"] will contain "on".
If there is no checkmark, then $_POST["mycheckbox"] will be unset.
If you use POST or GET, then on the next page, you can display the info... or do stuff like send emails with it...
e.g. for post u could have:
$HTTP_POST_VARS['name']; , meaning the field name from the form, and $name= being the variable...
e.g. for post u could have:
Code: Select all
<? $name=$HTTP_POST_VARS['name']; ?>For the checkboxes...
You just do the normal thing with them in the form..
e.g:
So if the form returned Male, then $gender would be Male, and if they chose female, then $gender would = female...
You just do the normal thing with them in the form..
e.g:
Code: Select all
<? $gender=$HTTP_POST_VARS['gender']; ?>Again, I want to let everyone know I greatly appreciate all the help.
Althought, something (probably painfully obvious) eludes me. I've been trying some testing, and nothing has been working. I guess the hint lies in why this doesn't work:
------------------------------test.php-------------------------------------
--------------------------------page.php---------------------------
This is sort of what doesn't work for me. The variable isn't getting defined as nothing comes up on the screen. I know it's probably obvious, but I'm missing it.
Thanks in advance,
Eric
Althought, something (probably painfully obvious) eludes me. I've been trying some testing, and nothing has been working. I guess the hint lies in why this doesn't work:
------------------------------test.php-------------------------------------
Code: Select all
<form action="page.php" method="post">
<?
print("<input type=radio name="test" . "" . variable="test" . "">" . test .
"</input>");
?>
<input type="submit">
</form>--------------------------------page.php---------------------------
Code: Select all
<?php
echo $_POSTї'variable'];
?>Thanks in advance,
Eric
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Code: Select all
<form action="page.php" method="post">
<?
print("<input type="radio" name="test" value="test">");
?>
<input type="submit">
</form>-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Yes, I was pushing the radio button.
I want to make sure I'm clear so I don't catch myself in an embarassing error and waste your time.
With what you've given me (the plain html) this is what I've tried:
------------------test.php----------------------------
-------------------page.php----------------------------
I tried it with and without the closing </input> tag. Neither worked. I also tried 'name' and 'value' in the $POST echo.
Thanks again,
Eric
I want to make sure I'm clear so I don't catch myself in an embarassing error and waste your time.
With what you've given me (the plain html) this is what I've tried:
------------------test.php----------------------------
Code: Select all
<form action="page.php" method="post">
<input type="radio" name="test" value="test"> test</input>
<input type="submit" value="Submit!">
</form>Code: Select all
<?php
echo $_POSTї'value'];
?>Thanks again,
Eric
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Radio buttons don't have a </input>.
Let's try this:
Then on page.php:
If that doesn't work, replace the above with:
Let's try this:
Code: Select all
<form action="page.php" method="post">
<input type="radio" name="mytestradiobutton" value="testvalue"> radio button description
<input type="submit" value="Submit!">
</form>Then on page.php:
Code: Select all
<?php
echo $_POSTї"mytestradiobutton"]; // should output testvalue
?>Code: Select all
<?php
echo $HTTP_POST_VARSї"mytestradiobutton"]; // should output testvalue
?>