Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/48343-solved-form-submits-even-when-canceled/
Share on other sites

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

}

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/>";
}

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']

 

 

 

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.