Jump to content


Photo

PHP submit not working


  • Please log in to reply
4 replies to this topic

#1 Xurion

Xurion
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts

Posted 24 August 2006 - 09:30 AM

Hi all.

I posted a problem with this code yesterday, and after it being moved around a bit to different areas of the site, I have come to the conclusion that my problem has something to do with the way my PHP code is submitting.

Simply put, you need to input the numbers that display and click the submit button. If correct you get a message saying they are correct, but if wrong you get a message teling you what the correct sequence was.

The problem is that when you input the number into the text field and press enter/return, the page seems to submit, but it does not execute the code as it should if you clicked the submit button. I tested it in Firefox and it seems to submit fine. Very confused at the moment.


Anyway the code is:

<?php
if($_POST[submit]){
	if($_POST[txtimagenumbers] == $_POST[txtcorrectsequence]){
		echo 'correct images!';
	} else{
		echo 'WRONG! correct sequence was :'.$_POST[txtcorrectsequence];
	}
} else{
	$codeimages = array('1','2','3','4','5','6'); //names of the images without the .gif extension
	$imagenumbers = array(rand(0,5),rand(0,5),rand(0,5),rand(0,5)); //generate the four random numbers deciding on what images to use.
	$content = '<html>
	<head>
	<title>Numbers</title>
	</head>
	<body>
	<table border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td><img src="'.$codeimages[$imagenumbers[0]].'.gif" border="0" alt=""></td>
		<td><img src="'.$codeimages[$imagenumbers[1]].'.gif" border="0" alt=""></td>
		<td><img src="'.$codeimages[$imagenumbers[2]].'.gif" border="0" alt=""></td>
		<td><img src="'.$codeimages[$imagenumbers[3]].'.gif" border="0" alt=""></td>
	</tr>
	</table>';
	$filename = basename($_SERVER['PHP_SELF']);
	$content .= '<form action="'.$filename.'" method="post">
		<input type="text" name="txtimagenumbers">
		<input type="hidden" name="txtcorrectsequence" value="'.$codeimages[$imagenumbers[0]].''.$codeimages[$imagenumbers[1]].''.$codeimages[$imagenumbers[2]].''.$codeimages[$imagenumbers[3]].'">
		<input type="submit" value="submit" name="submit">
	</form>
	</body>
	</html>';
	echo $content;
}
?>

Any help to a n00b would be great!  ::)

#2 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 24 August 2006 - 09:41 AM

I think the $_POST indicies need to be in quotation marks.  Instead of $_POST[submit] you need $_POST['submit'], but I'm not sure  ???

#3 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 24 August 2006 - 09:43 AM

Use a hidden field in your form, such as:
<input type="hidden" name="_submit" value="1">

then test this value in your POST array instead of 'submit', such as:
if(isset($_POST['_submit'])){

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#4 Xurion

Xurion
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts

Posted 24 August 2006 - 10:36 AM

Use a hidden field in your form, such as:

<input type="hidden" name="_submit" value="1">

then test this value in your POST array instead of 'submit', such as:
if(isset($_POST['_submit'])){

Ronald   8)

Thanks for that Ronald.

Your suggestion of using the hidden field worked, but the previous problems solution still eludes me. Strange how Firefox would submit the form. There are various forms online that I have looked at that use the same code and they work >_<

Thx again!

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 24 August 2006 - 10:44 AM

As far as I understand it from people who know more about IE quirks then I do, IE does accept the ENTER key press. But the value of a type=submit field is not passed. Values of type=text and type=hidden do get passed.

I don't know why, but that is also what I experience, sio I always use the hidden field to signal submission.

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users