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

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
artcab
Forum Newbie
Posts: 5
Joined: Sun Jul 11, 2010 2:47 pm
Location: Venezuela

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

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post 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";
}

?>
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

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

Post 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
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post 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
}
artcab
Forum Newbie
Posts: 5
Joined: Sun Jul 11, 2010 2:47 pm
Location: Venezuela

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

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post 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
artcab
Forum Newbie
Posts: 5
Joined: Sun Jul 11, 2010 2:47 pm
Location: Venezuela

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

Post by artcab »

Grrr, it just prints

Code: Select all

array()
that's not readable information for humans D:
thanks for the answers, :) btw..
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post 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!
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post 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?
artcab
Forum Newbie
Posts: 5
Joined: Sun Jul 11, 2010 2:47 pm
Location: Venezuela

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

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post by liljester »

can you post a link to the page in question so i can have a look at it live?
artcab
Forum Newbie
Posts: 5
Joined: Sun Jul 11, 2010 2:47 pm
Location: Venezuela

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

Post 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(.
**
Post Reply