PHP Form checking if submit has been pressed
I've been going insane the past 3 hours with this issue. I had this completely working.....and it actually still is, but on another page. The only issue is that on a different page it stops working. Here is the basic run down:
- User visit page, clicks on button, JavaScript ModalBox opens up.
- In the modal box is a form, which the user fills out and hits Send.
- The form action is itself so it returns and checks if isset() is true on the submit button.
This is the issue it seems. I got this working on another script, but it's over 5 pages long so here is the small script that I just can't for the life of me get to work:
<?php session_start(); ?>
<html>
<head>
<?php
if (isset($_GET['initForm'])){
echo 'Complete';
}
?>
</head>
<body>
<?php
echo '
<div style="padding:0;margin:0;float:left;height:120px;width:100%;">
<div align="center" width="50%" style="padding-top:2px;"><img src="images/step1.png"></div>
<div align="center" width="50%" style="font-face:Arial; font-size:14px;padding-top:4px;">Which package are you interested in?</div>
<div align="center" width="50%" style="font-face:Arial; font-size:14px;padding-top:6px;">
<FORM action="test.php" name="initForm" id="initForm" onsubmit="return false;">
<select name="packageName">
<option value="Weekend">Weekend - 4 Days/3 Nights</option>
<option value="Weekday">Weekday - 5 Days/4 Nights</option>
</select>
</div>
<div align="center" width="50%" style="font-face:Arial; font-size:14px;padding-top:6px;">
<input Type = "Submit" name="initForm" Value ="Send" onclick="Modalbox.show(\'test.php\', {title: \'Vacation Booking\', width: 600, params:Form.serialize(\'initForm\')}); return false;">
</FORM>
</div>
</div>';
?>
</body>
</html>
-----FIXED------Solution below:-------
^Th开发者_JS百科e tag had to be placed on the outermost part of the html and now I can access all the elements of the $_Get array, including the submit button. Can anyone explain why this is so picky? I want my 3 hours back. Well at least I'll hopefully never make a mistake like this again.
Forms are usually submitted via POST, but you are checking $_GET. Are you doing an AJAX request and sending the data via GET? If not, you should check:
if (isset($_POST['initForm'])){
...
I would do:
if (isset($_GET['initForm']) && $_GET['initForm'] == 'Send'){
Also, I would also probably specify the method
in the form element and use POST
instead of GET
, due to it's intransigence.
Also, why are you outputting echo 'Complete';
in the head
block? This is not right. It should be in the body
element (which may also be part of the problem).
精彩评论