确认并发布值后,重定向到另一页

问题描述:

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.

</div>

我有2个php文件。 其中一个具有变量值,另一个使用这些值。 我正在使用发布 strong>将值从一个文件传递到另一个文件。 p>

p>

 &lt; html&gt;  &lt; body&gt; 
 
&lt; form method =“POST”&gt; 
 
&lt; input type =“hidden”name =“firstname”value =“Saurabh”&gt; 
 
&lt; input type =  “hidden”name =“age”value =“22”&gt; 
 
&lt; input type =“submit”onclick =“javascript:completeAndRedirect();”&gt; 
 
&lt; / form&gt; 
 \  n&lt; script&gt; 
 
function completeAndRedirect(){
 
 
 var r = confirm(“按一个按钮!”); 
 
 if if(r == true){
 
 
nom.href ='  page2.php'; 
 
} 
 
} 
 
&lt; / script&gt; 
 
&lt; / body&gt;&lt; / html&gt;  code>  pre> 
 
   div> 
 
  div> 
 
 
 
 

p>

\ n
 &lt;?php 
 
 //使用POST 
 
  
 $ firstname = $ _POST ['firstname']; \  r 
 $ age = $ _POST ['age']; 
 
?&gt; 
 
&lt; html&gt; 
 
&lt; head&gt; 
 
&lt; / head&gt; 
 
&lt; body&gt;  
 
&lt; p&gt; 
 
第一名:&lt;?php echo“$ firstname”;  ?&GT;  &lt; br&gt; 
 
age:&lt;?php echo“$ age”;  ?&GT;  
 
&LT; / p为H. 
 
&LT; / BODY&GT; 
 
&LT; / HTML&GT; 代码>  PRE> 
 
  DIV> 
 
  DIV> 
  
 
 
 

但它不能按照我希望的方式工作。 只有在点击“ok”时才会重定向

它不会在任何地方重定向。 p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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>

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

    }

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>

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>

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