Forum Moderators: coopster

Message Too Old, No Replies

Help with $ POST['Submit'].

         

appletea

9:57 am on Jul 21, 2010 (gmt 0)

10+ Year Member



Hi,

Currently I have this code in which it would disabled the submit button once the submit button is pressed. HOWEVER, my problem is that I have few list of courses tht i have managed to fetch and each course once click on the link will have a save and a submit button in it.So if i pressed the submit button in the first course, this would also disable the second,third,etc course submit button too. Therefore I want to know on how to disabled both save and submit button once the submit button is being pressed for that particular course only. I'm using javascript for the submit button. Could anyone point out my mistakes? using session_destroy() doesn't seem to have any effect.


 session_start(); 
$disabled = "disabled='disabled'";
if ($role== 'user'){
if ($_POST['Submit'] =="" ) {
$disabled = "";
session_destroy();
}
}
if($_SESSION['Submit'] == "sent")
{
echo "Someone has pressed submit.Please seek System Adminstrator if any changes are to be made."; }


<input type= "Submit" name= "Save" value="Save">
<?php echo "<input type='Submit' name='Submit' value='Submit' onClick="return confirmSubmit()" ". $disabled ."/>";?>


In addition to tht,the submit button seem to have an error if i used the onclick part with $disabled.However if i were to remove the onclick part, it would be working perfectly fine.I wish to retain the onclick. Can anyone give suggestion to this also?Thanks.

Matthew1980

11:29 am on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there appletea,

In addition to tht,the submit button seem to have an error if i used the onclick part with $disabled

It will, primarily because the html is echoed, and you haven't escaped the javascript call ie:

echo "<input type=\"Submit\" name=\"Submit\" value=\"Submit\" onClick=\"return confirmSubmit();\" ".$disabled." />";

Not sure as it is critical but for xhtml you need the /> end part with a space from the closing quote of the last attribute.

As for your original issue, I *think* you may need to have them individually named so that you can differentiate between distinct buttons - though I'm not 100% on that, try a post in the javascript forum to see :)

Cheers,
MRb

rocknbil

6:04 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if i pressed the submit button in the first course, this would also disable the second,third,etc course submit button too.


First, let me ask you: what if someone is in a text field and they press enter on the form? The form will submit, but you get no click on the button. Move your confirmSubmit to the form.

$form = "
<form action=\"yourscript.php\" method=\"post\" onsubmit=\"return confirmSubmit(this);\">
<!-- etc. -->
<input type=\"Submit' name=\"SubmitButton\" value=\"Submit\"/>";
</form>
";
echo $form;

Never name objects possible reserved words, like Submit, Reset, Search, etc.

Note two important additions, return and this as a reference to the form. This refers to the form itself. So your JS will now need two things: specific references to the form, and a return false at the end on error. Return false is what will stop the form from submitting if there is an error, if no error, return true. This is also how you keep it from affecting other forms on the page. I don't know what the JS is doing, bur examine this, it will show what I mean.


function confirmSubmit(form) {
var msg='';
if (form.some_field.value=='') {
msg = "The some field is required.";
}
if (msg=='') {
form.SubmitButton.disabled=true;
return true;
}
else {
alert(msg);
return false;
}
}


As for the PHP part, if you have multiple forms on the page, you're just going to have to name the submit buttons different names - however note the comment above, if they press enter, I'm "pretty sure" there will be no $_POST['SubmitButton']. Safer to use a hidden field. like <input type="hidden" name="Form1" value="1">,

$disabled_buttons=Array();
$forms = Array('Form1','Form2','Form3','Form4');
foreach ($forms as $form) {
$disabled_buttons[$form] = (($role== 'user') and isset($_POST[$form]))?' disabled="disabled"':null;
}

... <input type=\"Submit\" name=\"SubmitButton\" value=\"Submit\"". $disabled_buttons['Form1'] ."/>";