Getting Data from Forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Getting Data from Forms

Post by Dowfen »

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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

It's often useful to try this when debugging a form submission script:

Code: Select all

<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Try a forum search for more info on using checkboxes.
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

Post by microthick »

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.
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

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:

Code: Select all

<? $name=$HTTP_POST_VARS['name']; ?>
$HTTP_POST_VARS['name']; , meaning the field name from the form, and $name= being the variable...
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

So basically the checkboxes aren't going to work for me because I'll need the name to do anything usefully with MySQL.

Right?

By the way, thanks for the replies so far. They're really helping me!
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

For the checkboxes...

You just do the normal thing with them in the form..

e.g:

Code: Select all

<? $gender=$HTTP_POST_VARS['gender']; ?>
So if the form returned Male, then $gender would be Male, and if they chose female, then $gender would = female...
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

you need to specify a value for the checkbox...

peace
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Yeah, so you have to set the value from the form...
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

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-------------------------------------

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&#1111;'variable']; 
?>
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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Code: Select all

<form action="page.php" method="post"> 
<? 
    
      print("<input type="radio" name="test" value="test">"); 

?> 
<input type="submit"> 
</form>
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

Dear god....

I'm either really, really stupid or that still doesn't work.

Eric
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Ok, let's do it plain html first.

<form action="page.php" method="post">
<input type="radio" name="test" value="test">
<input type="submit" value="Submit!">
</form>

And you are selecting the radio button before pressing Submit! right?
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

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----------------------------

Code: Select all

<form action="page.php" method="post"> 
<input type="radio" name="test" value="test"> test</input>
<input type="submit" value="Submit!"> 
</form>
-------------------page.php----------------------------

Code: Select all

<?php 
echo $_POST&#1111;'value']; 
?>
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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Radio buttons don't have a </input>.

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&#1111;"mytestradiobutton"]; // should output testvalue 
?>
If that doesn't work, replace the above with:

Code: Select all

<?php 
echo $HTTP_POST_VARS&#1111;"mytestradiobutton"]; // should output testvalue 
?>
Post Reply