Jump to content

Problem with PHP forms


milesap

Recommended Posts

Sorry if this is an extremely easy question, but sometimes it takes another set of eyes to spot a problem. Alright, so I have an HTML form which posts to a PHP file for processing. My form has two buttons, one which is 'Save and Continue' and the other is 'Save and Logout'. I cannot figure out how to determine which button was pressed from within my PHP file.

 

Here's the relevant code I have:

...

<div class="buttons">
	<button type="submit" class="positive" name="contactButton" value="SC">
		Save and Continue Registration
	</button>
	<button type="submit" class="neutral" name="contactButton" value="SL">
		Save and Logout
	</button>
</div>

...

 

In my PHP file I ran:

 

print_r ($_POST);

 

to see what was posted to my script. Sure enough all the input fields showed up correctly, but the button was no where to be found. The button name nor it's value was in the $_POST.

 

Does anyone know if I'm doing something wrong here? I tried using the oldschool style buttons as well, <input type="submit" name="contactForm" value="1" /> but I get the same result.

 

Thanks so much in advance!

 

Link to comment
https://forums.phpfreaks.com/topic/152023-problem-with-php-forms/
Share on other sites

you could go about it a different way...instead of having two submit buttons just have one and then have a couple radio buttons..one for save and contine and one for save and logout.

<input type='radio' name='option' value='sac'>Save and Continue 
<input type='radio' name='option' value='saq' checked>Save and Quit

 

then make sure one has selected one

if(!isset($_POST['option']))
   {
$errormessage = "Please select an option.";
   extract($_POST);
   include("yourform.php");
   exit();
   }

 

then embed this in your form page

if(isset($errormessage))
{
echo $errormessage;
}

 

back to your processing file...

if they have selected, set up another if stmt

if($_POST['option']=="sac")
{
code that saves and continues.
}
else
{
code that saves and logs out.
}

I agree with dadamssg. It is an alternative approach.

 

In any case you want to do it with a button anyway, you may add a hidden field where you will check the changes of the value. That for every time someone click either of the buttons, using javascript, you are going to change the value in that hidden field, say on the other button is 1 and the other is 2. Then you will check the value of the hidden field on the PHP side, that should do it.

 

cheers,

Jay

Thanks for the replies, and thanks bluejay002, you put my on the right track. Unfortunately I needed to use the buttons and could not use radio boxes. Anyways for those who are interested I found a solution. IE has problems handling multiple submit buttons, and FireFox won't assign a name or value to the button. So a clever javascript along with a hidden element will do the trick. Of course it's never good to assume all users will have javascript enabled, you can compensate for this in your PHP file by having a default action take place if the hidden element is left empty (thus the javascript was disabled).

 

...

<div class="buttons">
	<button type="submit" class="positive" onClick="javascript: submitLink(document.formName, 'hiddenElelmentName', 'hiddenElementValueYouWantToAdd');"> 
               Save and Continue Registration
	</button>
	<button type="submit" class="neutral" onClick="javascript: submitLink(document.formName, 'hiddenElelmentName', 'hiddenElementValueYouWantToAddDifferent');">
		Save and Add Another Address
	</button>
</div>

...

 

And finally the submitLink javascript:

 

<script type='text/javascript'>
function submitLink(theForm, theName, theValue) {
theForm.elements[theName].value = theValue;
}
</script>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.