1

I have created a form below and some javascript code in php, the it doesnt redirect to other pages.

<form method="post" name="for">
<?php

echo "<input type='text' name='text1'><input type='submit' name='submit' value='go' onclick='fon1();'>";
?>
</form>

<?php
        echo "<script type='text/javascript'>";
        echo "function fon1(){";
        echo "var k = confirm('Confirm Delete');";
        echo "if(k == true){";
        echo "window.location = 'http://www.google.com'; }";
        echo "else{ window.location = 'http://www.yahoo.com';} ";
        echo "}</script>";
?>

7 Answers 7

1

Put a "return false;" after calling fon1();

echo "<input type='text' name='text1'><input type='submit' name='submit' value='go' onclick='fon1(); return false;'>";
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, Its redirecting to page, May i know why? and how it worked.
the type="submit" bypassed your javascripts, in order to suppress the default behavior of the browser we must cancel the current behavior which is submit. You can also change the type to button, and remove the return false statement.
0

Use

window.location.href

instead of

window.location

Comments

0

I think you change the type of your form buttom from "submit" to "button" :

echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";

try this!

1 Comment

With submit button, I need a value from it.
0

you can use window.location.replace to simulate a redirect (back button won't work) or window.location.href so simulate a click

Comments

0

Do this:

<form method="post" name="for">
<?php

echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='return fon1();'>";
?>
</form>

<?php
        echo "<script type='text/javascript'>";
        echo "function fon1(){";
        echo "var k = confirm('Confirm Delete');";
        echo "if(k == true){";
        echo "location.href = 'http://www.google.com'; }";
        echo "else{ window.location = 'http://www.yahoo.com';} ";
        echo "}";
?>

Comments

0
<form method="post" name="for">
<?php

echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";
?>
</form>


     <script type='text/javascript'>
        function fon1(){
        var k = confirm('Confirm Delete');
        if(k == true){
       document.location = 'http://www.google.com';
         }
        else
        { 
        document.location = 'http://www.yahoo.com';
        } 
        }</script>

Comments

0

Just use this..

<form method="post" name="for">
    <?php
    echo "<input type='text' name='text1'><input type='button' name='submit' value='go' onclick='fon1();'>";
    ?>
    </form>



 <script type='text/javascript'>
        function fon1(){
            if(confirm('Confirm Delete')) {;
                document.location.href = 'http://www.google.com';
            }
            else { 
                document.location.href = 'http://www.yahoo.com';
            } 
        }
    </script>

Comments

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.