Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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(......);

});

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.

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

Try:

if (r==true) {
    window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
    window.location = "PAGE THAT YOU WANT"; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top