Why doesn't this form work (I've broken down)

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

Why doesn't this form work (I've broken down)

Post by Dowfen »

Hello,

I posted about this problem a couple weeks ago and have been very busy between then and now with work so I havn't had the opportunity to work on my project very much, so I was hoping someone could tell me why this form doesn't work:

Code: Select all

<form action="deletelinks.php" method="post">
<?
	**MYSQL CONNECT STUFF REMOVED**
	
	print("<strong>" . "Please select the links you'd like to delete:" . 

"</strong><br></br>");

	while ($row = mysql_fetch_array($result)) &#123;

		print("<input type=checkbox value="" . $row&#1111;"title"] .   "">" . $row&#1111;"title"] 

. "</input><br>");
&#125;
?>	
		<center><input type="submit"></center>
		</form>
------------------------------deletelinks.php-------------------------------

Code: Select all

<?php 
echo $_POST&#1111;'value']; 
?>

I'm just trying to get the form to send and output some data so I can move on. I simply have not been able to figure this out.

Again, I really appreciate everyone's help.

Eric
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you check box is missing name="" part.
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

Yeah, I've had it with a name, without a name, etc. it still does not work.

I appreciate the reply though,

Eric
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

what the output from the first code section looks like?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Try this and see what happens. Dump the code into one php file and run it.

Code: Select all

<?php

if(count($_POST)>0)
{
     $inputValue = $_POST["myInput"];
     echo "Hmmmm tasty <b>{$inputValue}</b>.<hr />";
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select name="myInput">
<option value="Apples">I like Apples</option>
<option value="Bananas">Monkies like Bananas</option>
</select>

<br />

<input type="submit" value="submit" />

</form>
Last edited by Gen-ik on Thu Dec 18, 2003 9:00 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

did you mean

Code: Select all

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
? ;)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

oh yeah (doh!).... changed it.

That's what happens when you try and code at 3 in the morning.
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

That works and outputs "Hmmm tasty Apples."

I do not understand why mine doesn't. When you view the source of the page it looks just like a normal form. With a "name" and "value" value.

I appreciate all the help guys,

Eric
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Don't forget that a checkbox will only post it's value with a form if it is checked.

If you are using multiple inputs with the same name don't forget to put square brackets after each name, for example..
name="box[]" value="linkOne"
name="box[]" value="linkTwo"
..you will then find that $_POST["box"] is an array and not a single value.

On the page that you submit the form to add var_dump($_POST) and see what it chucks out. You will then be able to see if the values are actually getting to PHP ok.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

To summarise, your code would look something like this...

Code: Select all

<?php

//**MYSQL CONNECT STUFF REMOVED** 

echo '<form action="deletelinks.php" method="POST">';
echo '<strong>Please select the links you''d like to delete:</strong><br>';

while ($row = mysql_fetch_array($result)) {

	$row["title"] = htmlspecialchars($row["title"]);

	echo '<input type="checkbox" [b]name="links[]"[/b] value="'.$row["title"].'"> '.$row["title"].'<br>';
} 

echo '<center><input type="submit"></center>';
echo '</form>';

?>
Notice I've done a htmlspecialchars to the title, otherwise, any title with double quotes in it will break your page...

And deletelinks.php...

Code: Select all

<?php

if (is_array($_POST["links"]) {
	foreach ($_POST["links"] as $value) {
		echo $value.'<br>';
	}
}
else {
	echo 'No boxes checked.';
}

?>
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

Unexpected '{' on line 3 on the deletelinks.php page.

Everything looks right to me...but then again I know little. <grin>

Eric
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Hmm, tasty unbalanced parenthesis ;)
should be:

Code: Select all

if (is_array($_POST["links"])) { 
   foreach ($_POST["links"] as $value) { 
      echo $value.'<br>'; 
   } 
} 
else { 
   echo 'No boxes checked.'; 
}
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

I'm sorry you had to point that out. :oops: I need to work on this stuff when I'm semi-conscious.

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

Post by Dowfen »

choppsta wrote:To summarise, your code would look something like this...

Code: Select all

<?php

//**MYSQL CONNECT STUFF REMOVED** 

echo '<form action="deletelinks.php" method="POST">';
echo '<strong>Please select the links you''d like to delete:</strong><br>';

while ($row = mysql_fetch_array($result)) {

	$row["title"] = htmlspecialchars($row["title"]);

	echo '<input type="checkbox" [b]name="links[]"[/b] value="'.$row["title"].'"> '.$row["title"].'<br>';
} 

echo '<center><input type="submit"></center>';
echo '</form>';

?>
Notice I've done a htmlspecialchars to the title, otherwise, any title with double quotes in it will break your page...

And deletelinks.php...

Code: Select all

<?php

if (is_array($_POST["links"]) {
	foreach ($_POST["links"] as $value) {
		echo $value.'<br>';
	}
}
else {
	echo 'No boxes checked.';
}

?>
I was concerned about that, but hadn't gotten to that point. However, with the special chars things like apostrophes get output wrong as well. I don't suppose there's anyway to correct this (simply) while still keeping the check for "" in?
Dowfen
Forum Newbie
Posts: 19
Joined: Thu Dec 11, 2003 1:22 pm

Post by Dowfen »

Amazingly I was able to fix it on my own. I appreciate all the help guys. You helped me get my page working. I'll finally let this thread die.

Eric
Post Reply