Irshad
Irshad

Reputation: 222

Redirect to another page after confirm and post values

I have 2 php files. One of them has values for the variables and the other uses those values. I am using post to pass value from one file to another.

<html><body>
<form method="POST">
    <input type="hidden" name="firstname" value="Saurabh">
    <input type="hidden" name="age" value="22">
	<input type="submit" onclick="javascript:completeAndRedirect();">
</form>
<script>
function completeAndRedirect(){
	var r = confirm("Press a button!");
    if (r == true) {
        location.href='page2.php';
    }
	}
</script>
</body></html>

<?php
//Using POST
$firstname = $_POST['firstname'];
$age = $_POST['age'];
?>
<html>
<head>
</head>
<body>
<p>
first name : <?php echo "$firstname"; ?> <br>
age :			<?php echo "$age"; ?> 
</p>
</body>
</html>

But it isn't working the way i want it to work. It should redirect only when "ok" is clicked
IT DOES NOT REDIRECT ANYWHERE.

Upvotes: 1

Views: 7068

Answers (5)

Bruce Tong
Bruce Tong

Reputation: 1431

* If you are using Laravel 5.8*

<form action="/appraisal/delete/{{$task->id}}" method="post" id="deleteForm">
{{ csrf_field() }}
{{ method_field('delete') }}
<button type="submit" id="delete" class="btn btn-sm btn-danger">Delete</button>
</form>


<script>
var beforeDelete = function(event) {
event.preventDefault();
var retVal = window.confirm('This will be deleted. Are you sure?');

if(retVal == true) { document.getElementById('deleteForm').submit(); }
else { event.preventDefault(); }
}

var btn = document.getElementById('delete');
var res = btn.addEventListener('click', beforeDelete);
</script>

Upvotes: 0

Jalpa
Jalpa

Reputation: 720

I got your issue please set form action and put location in window.href.

Please replace below code.

<html><body>
        <form method="POST" action="javascript:void(0);" name="myForm" id="myForm">
    <input type="hidden" name="firstname" value="Saurabh">
    <input type="hidden" name="age" value="22">
    <input type="submit" onclick="javascript:completeAndRedirect();">
</form>
<script>
function completeAndRedirect(){
    var r = confirm("Press a button!");
    if (r == true) {
        document.getElementById("myForm").action = "page2.php";
       document.getElementById("myForm").submit();
    }
    }
</script>
</body></html>

Upvotes: 2

Vasim Shaikh
Vasim Shaikh

Reputation: 4512

I am use this for delete functionalty it may be helpful to you,

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery.confirm.js"></script>
$(document).ready(function(){
$('#add').click(function(){
var id = $("#get_id").val()

$.confirm({
            'title'     : 'Confirmation',
            'message'   : 'You are about to send data of this User.Continue?',
            'buttons'   : {
                'Yes'   : {

                    'action': function(){$a.ajax({
    url: 'http://localhost:8080/page2.php
    type: 'POST',
    data: { firstname:firstname ,age:age }
        success: (function(){
        alert('added' +firstname );

    });
                    }


                },
                'No'    : {

                    'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
                }
            }
        });

});
});


<form id="basket" method="post" action="">
    <input type="hidden" name="firstname" value="Saurabh">
    <input type="hidden" name="age" value="22">
    <input type="submit" id="add" Value="add">
</form>

Upvotes: 0

Vasim Shaikh
Vasim Shaikh

Reputation: 4512

In first file is working,Check this one.

<html>
<script>
function letstart()
{

    var r = confirm("Press a button!");
    if (r == true) {
        location.href='http://localhost:8080/stackflow/page2.php';
    }

}
</script>
</head>
<body>
<input type="button" value="Click me" onclick="letstart()">

</html>

Upvotes: 0

Gagan Upadhyay
Gagan Upadhyay

Reputation: 255

use return parameter while using onclick buttone:

</script>
<form method="POST">
    <input type="hidden" name="firstname" value="Saurabh">
    <input type="hidden" name="age" value="22">
    <input type="submit" onclick="return completeAndRedirect();">
</form>

Add return false in js function.

function completeAndRedirect(){
    var r = confirm("Press a button!");

    if (r == true) {alert(r);
        window.location.href='test.php';
        return false;
    }

    }

Upvotes: 0

Related Questions