Dragen Posted April 23, 2007 Share Posted April 23, 2007 okay.. not sure if this should be in php or javascript section.. I've got a form mostly written in php. above the form is: <?php if ($_SERVER['REQUEST_METHOD'] != 'POST'){ ?> then the form, so it only displays the form if the method is not post.. because if it is post the form would have been sent. The form starts: <form name="rem" method="post" onsubmit="confirmdel();" action="<?php echo $_SERVER['PHP_SELF']; ?>"> As you can see it's sending the form to itself. That why I have the above != 'POST' part. If the form is sent it echoes form sent etc.. The onsubmit="confirmdel();", part is supposed to bring up a confirmation box when you press submit. Then if you cancel it doesn't send the form. here's the javascript: <script language="javascript" type="text/javascript"> function confirmdel() { var messageb = "Are you sure you wish to delete these items?"; if (confirm(messageb)) return true; else return false; } </script>"; The problem is when I press submit the warning box appears, but even if I click cancel it still submits the form. I think it's because of how I'm displaying the form with the <?php if ($_SERVER['REQUEST_METHOD'] != 'POST'){ ?> is there a better way to submit a form to itself? or just a solution to the problem I've got? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/ Share on other sites More sharing options...
benjaminbeazy Posted April 23, 2007 Share Posted April 23, 2007 not sure if this helps but this is how i do it sometimes, doesn't work however if you're posting to get to this page... <?php // if form hasn't been posted, create a var for form display if(! $_POST){ $error = "Please complete the form below..."; } if($_POST){ // do your form validating and such all in here and generate error codes if invalidate input, for example if(strlen($_POST['password']) < 6){ $error['password'] = "Password must be at least 6 characters"; } } if($error){ //display errors echo "Some errors exist. Please review and correct. <br/>"; foreach($error as $var => $val){ echo "$val <br/>"; } // display form }else{ //return results } Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236361 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Author Share Posted April 23, 2007 thanks.. I'm getting this error though: Warning: Invalid argument supplied for foreach() in /home/fhlinux181/g/gimppro.co.uk/user/htdocs/admin/edit.php on line 58 line 58 is the foreach() line: echo "Some errors exist. Please review and correct.<br />"; foreach($error as $var => $val){ echo $val . " <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236401 Share on other sites More sharing options...
benjaminbeazy Posted April 23, 2007 Share Posted April 23, 2007 sorry, you have to define $error as an array if you do it like that, so $error['noPost'] = "Please fill out form."; and any errors you get in the form validating need to be in the form of $error['VAR'] Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236402 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Author Share Posted April 23, 2007 okay, thanks. That works well, with the validating although the javascript window that appears to clarify whether I want bto send the form or not, stil doesn't make a difference. My form deletes information from a database, so I was using the javascript warning as a "are you sure you want to delete this" box. Problem is, even if I click cancle it still deletes the information... Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236406 Share on other sites More sharing options...
Wildbug Posted April 23, 2007 Share Posted April 23, 2007 This is JavaScript. If I remember correctly you need to do: <form onSubmit="return confirmdel()"> ... </form> Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236412 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Author Share Posted April 23, 2007 I can't believe how simple that was... I'd siply missed out the return part and just had <form onSubmit="confirmdel();"> That's why!! thanks. Quote Link to comment https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/#findComment-236414 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.