Page 1 of 1

isset($_POST['xxx']) not working in form generated by PHP

Posted: Sun Jul 11, 2010 3:18 pm
by artcab
Hello,
I have an HTML FORM:

Code: Select all

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
/* ... */
<input type="submit" value="show" name="submit" />
</form>';
then, I'll generate another form after the previous submit button is pressed:

Code: Select all

if(isset($_POST['submit'])){
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='submit' class='button' value='delete' name='submit2' />
/* perform actions... */
</form>";
}
that works just fine... the problem is that this new submit button called "submit2" will never be set... why?
I mean...

Code: Select all

if(isset($_POST['submit2'])){
/* this actions will never be executed... isset does not detect that the submit2 button was pressed */
}
Can you help me find what am I doing wrong?
thank you.

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 3:47 pm
by liljester
using the following file, $_POST['submit2'] was set and printed "submit2 was set". did i misunderstand your question?

Code: Select all

<?php

echo 'form1<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="submit" value="show" name="submit" />
</form>';
 
if(isset($_POST['submit'])){
	echo "form2<form action='".$_SERVER['PHP_SELF']."' method='post'>
	<input type='submit' class='button' value='delete' name='submit2' />
	</form>";
}
 
if(isset($_POST['submit2'])){
	echo "submit2 was set.<br />\n";
}

?>

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 5:59 pm
by morris520
Hi

i think something wrong with your isset statement
I would only use

Code: Select all

if($_POST['submit']){ }
Just double check the definition of isset function php.net/manual/en/function.isset.php

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 6:58 pm
by liljester
no there is nothing wrong with using isset() in this way. as i stated, the code i used above did work as expected using isset(). im not sure if the code i have is what the original poster is interested in.

that being said, you can check the value of $_POST['submit'] as morris suggested. you can even use several submit buttons with the same name on the same page if you give them different values. just be sure to check wich value was submitted.

Code: Select all

if($_POST['submit'] == "show") {
    // show something
} elseif($_POST['submit'] == "delete") {
    //  delete something
}

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:02 pm
by artcab
Yes, liljester trying to obtain $_POST values from generated buttons will always works, I thought that that had to be the problem,
I've tryied to check everything, this is the script that must be causing the error, but works...

Code: Select all

<?php
$conn = mysql_connect($site,$user,$pass) or die(mysql_error());
mysql_select_db("db",$conn) or die(mysql_error());

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
	<label>Eliminar fotos de:</label><br /><select name="evento">';
	$queryResult = mysql_query("SELECT * FROM eventos");
	if(mysql_num_rows($queryResult)==0){
		echo "<option>No hay Eventos</option>";
	} else {
		while($result = mysql_fetch_assoc($queryResult)){
			echo '<option>'.$result["eventotitulo"].'</option>';
		}
		mysql_free_result($queryResult);
	}
echo '</select><br /><br />
<input type="submit" class="button" value="Mostrar Fotos" name="submit2" />
</form>';

if(isset($_POST['submit2'])){
	$evento = $_POST['evento'];
	$queryResult = mysql_query("SELECT * FROM eventos WHERE eventotitulo='".$evento."'");
	if(mysql_num_rows($queryResult)==0){
		echo "No hay Eventos";
	} else {
		while($result = mysql_fetch_assoc($queryResult)){
			$fotos = $result['eventofotos'];
			$descripcion = $result['eventodescripcionfotos'];
			$fotos = explode(',',$fotos);
			$descripcion = explode('&#44',$descripcion);
			$i = "";
			echo "Haz click sobre cualquier imagen para eliminarla permanentemente.<br /><br /><br />";
			echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>";
			foreach($fotos as $i){
			echo "<label>eliminar<input type='checkbox' name='eliminar".$i."'></label><br />
				  <img src='../images/eventos/".$i."' width='50' height='50' />
				  <input type='text' name='desc".$i."' size=40 /><label>Descripcion Nueva</label>";
			}
			echo "<input type='submit' class='button' value='Actualizar Descripciones' name='submit3' />
				  </form>";
I've checked and the code above seems to works perfectly, but:

Code: Select all

if($_POST['submit3']){
	/* this won't execute */		
?>
I've even rewritten the whole previous script in order to make it do the same functions, but using $_GET values, from the URL, but it won't detect them neither.
I also tried to debug it using FireBug, and the Chrome's development tools, and everything seems to be right, there's no HTML errors.

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:11 pm
by liljester
just for fun, put this at the top of your script:

Code: Select all

print_r($_POST);
that will list contents of the $_POST array on every page load

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:22 pm
by artcab
Grrr, it just prints

Code: Select all

array()
that's not readable information for humans D:
thanks for the answers, :) btw..

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:26 pm
by liljester
i beg to differ :)

are you sure you typed "print_r($_POST);" and not just "print($_POST);" ?

http://us.php.net/manual/en/function.print-r.php
it says right at the top it outputs stuff readable by humans!

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:36 pm
by liljester
i have copy + pasted your code and taken out the db stuff.. it seems to be working. are you putting all your code in one file? or do you have more than one file?

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:46 pm
by artcab
liljester wrote:i beg to differ :)

are you sure you typed "print_r($_POST);" and not just "print($_POST);" ?

http://us.php.net/manual/en/function.print-r.php
it says right at the top it outputs stuff readable by humans!
Yes, I used print_r(), it's just weird.
And yes, all my code is in just 1 page.

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:48 pm
by liljester
can you post a link to the page in question so i can have a look at it live?

Re: isset($_POST['xxx']) not working in form generated by PH

Posted: Sun Jul 11, 2010 7:53 pm
by artcab
Thanks for everything,
I think that I'd better delete the part of the code that won't work, and try to implement it again later, in another file.
Again thanks for your time :D

edit: I haven't uploaded it to a web server yet x(.
**