Jump to content

[SOLVED] How do I SUBMIT a form..


A JM

Recommended Posts

I want to ask the user a question and receive an answer before SUBMITTING my form how would I go about achieving that?

 

For example: Before saving my forms information to the DB I want to ask the user with a pop-up if they are ready to finalize the document, wait for their answer, yes/no and save their choice as a variable.

 

Can this be done via PHP or is this more of a javascript issue?

 

Thanks.

 

A JM,

 

 

Link to comment
Share on other sites

It can be done either way.  In javascript you need to hook the form's submit event.  You can then display a dialog popup like this:

 

<script>
function onSubmit() {
    if (confirm("Do you really want to do that?")) {
        // submit the form
    } else {
        // don't submit the form
    }
}
</script>

 

Alternatively, you can store the user's answers to the form in the $_SESSION, that way you can display an intermediary page for them to confirm.

Link to comment
Share on other sites

Thanks for the posts.

 

I'm still a bit green with working with forms, php,etc.

 

From an implementation standpoint,  I think the the full script makes more sense.

 

The button on my form should still be a SUBMIT correct? How do I exit the script without submitting the form just don't process a SUBMIT?

 

Will look something like this?

 

<script>
function onSubmit() {
    if (confirm("Do you really want to do that?")) {
        document.getElementById("form_id").submit();
    } else {
        // don't submit the form
    }
}
</script>

 

 

 

Link to comment
Share on other sites

You do something like..

 

onclick="if(confirm('Are you sure? This is irreversible!')){return true;}else{return false;}"

 

Makes this easier. ;)

 

Makes sense, this runs from the button as opposed to the head and is more compact.

 

How do I capture the value from return whether it be TRUE/FALSE or is 'return' the variable that I can use?

 

A JM,

Link to comment
Share on other sites

You do something like..

 

onclick="if(confirm('Are you sure? This is irreversible!')){return true;}else{return false;}"

 

Makes this easier. ;)

 

Makes sense, this runs from the button as opposed to the head and is more compact.

 

How do I capture the value from return whether it be TRUE/FALSE or is 'return' the variable that I can use?

 

A JM,

 

That's all you need. Put that inside your Submit button element. Once submit is clicked you'll be asked that question, if you hit "Ok" It'll submit the form. If you hit "Cancel" It won't.

Link to comment
Share on other sites

OK, gotcha.

 

This doesn't return a variable of TRUE/FALSE only waits for the user to click YES/NO.

 

To return a variable with TRUE/FALSE I need to go with the script in the head section.

 

<button onclick="onSubmit()">Submit Form</button>

 

<script>
function onSubmit() {
    if (confirm("Do you really want to do that?")) {
        $myYESvariable = 'TRUE';
        document.getElementById("form_id").submit();
    } else {
        $myNOvariable = 'FALSE';
        // don't submit the form
    }
}
</script>

Link to comment
Share on other sites

OK, gotcha.

 

This doesn't return a variable of TRUE/FALSE only waits for the user to click YES/NO.

 

To return a variable with TRUE/FALSE I need to go with the script in the head section.

 

<button onclick="onSubmit()">Submit Form</button>

 

<script>
function onSubmit() {
    if (confirm("Do you really want to do that?")) {
        $myYESvariable = 'TRUE';
        document.getElementById("form_id").submit();
    } else {
        $myNOvariable = 'FALSE';
        // don't submit the form
    }
}
</script>

Why do you need to set anything to true or false?.. All you need to do is put the code I gave you inside your submit button: eg. <input type="submit" onclick="what I gave you..." /> And it'll do exactly what you want.

Link to comment
Share on other sites

I'm trying to set a value for a variable for TRUE $myvariable =1 and for FALSE $myvariable=0.

 

The script worked perfectly - last question on this...

 

My form has the following action:

 

<form action="<?php echo $editFormAction2; ?>" method="post" id="form1" enctype="multipart/form-data">

 

When the form is submitted by document.getElementById("form_id").submit(); will it run the action part of the form or do I need to kick that off in he script somehow <?php echo $editFormAction2; ?>?

 

Thanks.

Link to comment
Share on other sites

This doesn't seem to be working the way that I think it should so I must not be doing this correctly, hopefully someone can straighten me out...

 

My button on my form has the following:

 

<input type="button" name="submit_revision" value="Save Changes" onClick="onSubmit()"/>

 

my form:

 

<form action="<?php echo $editFormAction2; ?>" method="post" id="form1" enctype="multipart/form-data">

 

....other PHP stuff....

<script>
function onSubmit() {
    if (confirm("Finalize for Management review?")) {
        $submit_for_review = 1;
        document.getElementById("form1").submit();
	//confirm($submit_for_review);
	//exit();
    } else {
        $submit_for_review = 0;
        // don't submit the form
	//confirm($submit_for_review);
	//exit();
    }
}
</script>
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$editFormAction2 = $_SERVER['PHP_SELF'];
print_r("it submitted");
exit;
}
?>

 

My interpretation of the above is that when the form is submitted it would run $editFormAction2 but it isn't....???? not sure why though.

 

Also, the confirm() can it be a "YES"/"NO" as opposed to an "OK"/"CANCEL"???

 

Thanks,

Link to comment
Share on other sites

Variables set by javascript are not directly accessible by PHP. You need to use an AJAX call in the background to be able to do that.

 

Another point is that this popup isn't going to work for any users who don't have javascript enabled, so you may want to build a php fallback if it is that important.

Link to comment
Share on other sites

OK, makes sense.

 

So, if I use a hidden textbox and set the variable from Javascript that should work correct?

 

<script>
function onSubmit() {
    if (confirm("Finalize for Management review?")) {
document.getElementById("form1").elements["submit_for_review"].value = '1';
        document.getElementById("form1").submit();
    } else {
      document.getElementById("form1").elements["submit_for_review"].value = '0';
    }
}
</script>

 

Any ideas about

Also, the confirm() can it be a "YES"/"NO" as opposed to an "OK"/"CANCEL"???

 

 

Link to comment
Share on other sites

K, finally figured this out using javascript. Thanks to all that helped.

 

<script>
function onSubmit() {
    if (confirm("Finalize for Management review?")) {
        document.getElementById("form1").elements["submit_for_review"].value = '1';
        document.getElementById("form1").submit();
    } else {
      document.getElementById("form1").elements["submit_for_review"].value = '0';
      document.getElementById("form1").submit();
    }
}
</script>

 

I also realized that PHP script is read from top down (duh!) so if I wanted a script to run prior to another I just had to load it at the top of the page...

 

A JM,

 

Link to comment
Share on other sites

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.