1

What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.

<?php
    $username = 'joeblow';
    require_once ("/mypath/include/connnect_db.php");
?>

    <p>Please only submit this after above form is completely signed.</p>
    <form id="item_complete" method="post">
      <input name="submit" type="submit" form="item_complete"  value="Submit After Signing">
    </form>
<?php
    if(isset($_POST['submit'])) {  //if the submit button is clicked
        $complete = 'c';
        $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
        mysqli_query($con,$query) or  die("Cannot Update");
        echo "<script> confirmFunction(); </script>";
    }
    require_once ("/mypath/include/disconnect_db.php");
?>
    <script type="text/x-javascript">
        function confirmFunction(){
            var r=confirm("Confirm that you have signed the form!");
            if (r==true){
              window.open("http://smartpathrealty.com/smart-path-member-page/");
              }
            else {
              }
            }
    </script> 

My problem is the javascript function does not execute after the php updtates the database.

I appreciate any advice or comments you have about this.

2
  • PHP executes on the server. Javascript executes on the client. What you want is impossible. You can NOT have server-side PHP code trigger a client-side alert and then WAIT for the client to respond. Commented Mar 3, 2014 at 21:26
  • Is there anyway to redirect to back to a page after the user submits the form. I am very open to suggestions. Commented Mar 3, 2014 at 21:37

5 Answers 5

2

The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">

You can do the following:

Move function up and fix x-javascript:

<?php
    $username = 'joeblow';
    require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
    <input name="submit" type="submit" form="item_complete"  value="Submit After Signing">
</form>
<script type="text/javascript">
    function confirmFunction(){
        var r=confirm("Confirm that you have signed the form!");
        if (r==true){
          window.open("http://smartpathrealty.com/smart-path-member-page/");
          }
        else {
          }
        }
</script> 
<?php
if(isset($_POST['submit'])) {  //if the submit button is clicked
    $complete = 'c';
    $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
    mysqli_query($con,$query) or  die("Cannot Update");
    echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>

Fiddle: Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

This helped me find a solution that I post on bottom.
Okay, great. I'm glad I could help.
1

You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.

Heres a jquery example:

$.post('/myscript.php', {foo: 'bar'}).done(function (response) {

window.open(......);

});

1 Comment

Sorry, I am not followign this at all.
0

I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.

echo "<script> window.onload=confirmFunction(); </script>";

Hope this what you are looking for.

2 Comments

This loads the confirmation before the submit button is clicked.
I meant to add it the echo from your code snippet. You only have 1 which is inside if(isset($_POST['submit'])) {.....}, why would it run by default ?
0
<script type="text/javascript">
    function confirmFunction(){
        var r=confirm("Confirm that you have signed the form!");
        if (r==true){
          window.open("http://smartpathrealty.com/smart-path-member-page/");
          }
        else {
          }
        }
</script> 
<?php
if(isset($_POST['submit'])) {  //if the submit button is clicked
    $complete = 'c';
    $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
    mysqli_query($con,$query) or  die("Cannot Update");
    echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>

<script type="text/javascript">


    window.onload = function(){
        <?php
            if(submitted){
                echo "confirmFunction();";
            }
        ?>
    }
</script> 

2 Comments

This loads the confirmation before the submit button is clicked.
@user3376610 I updated my answer for your single page solution. The idea is to make the content of window.onload dependent on a variable set when your SQL transaction completes. Of course, multiple page solutions like others suggested would be much cleaner.
0

Try:

if (r==true) {
    window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
    window.location = "PAGE THAT YOU WANT"; 
}

1 Comment

This part of script already works correctly, just trying to figure out how after the person submits the form and all the php is executed next the user will see a pop up confirmation window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.