【问题标题】:Redirect to another page after confirm and post values确认并发布值后重定向到另一个页面
【发布时间】:2016-05-07 03:15:00
【问题描述】:

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

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

但它并没有按照我想要的方式工作。 仅当单击“确定”时才应重定向
它不会在任何地方重定向。

【问题讨论】:

标签: javascript php jquery html post


【解决方案1】:

我遇到了您的问题,请设置表单操作并将位置放在 window.href 中。

请替换以下代码。

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

【讨论】:

  • @Irshad Ohh 你还需要传递值吗?
  • @Irshad 一会儿我会更改我的答案中的代码。
  • @Irshad 我已经编辑了我的答案,请尝试并告诉我。
  • @Irshad 我现在更新了代码。请复制最新的代码。我也发现了同样的问题,我已经做出了改变。
  • 现在它没有传递值。
【解决方案2】:

在使用 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>

在js函数中添加return false。

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

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

    }

【讨论】:

  • 您没有返回任何内容并使用 SUBMIT 按钮,因此每次提交当前页面时......所以您必须使用 return false 来防止提交当前页面;
  • 它不允许我传递值
  • 因为你没有提交表单......你正在重定向页面......所以你需要传递值............window.location.href ='page2.php?firstname=fistname.....
【解决方案3】:

在第一个文件中,检查这个。

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

【讨论】:

  • 这不是我想要的。我需要传递隐藏的值
  • 真正想要做什么?
  • 你可以使用隐藏字段控制
【解决方案4】:

我将其用于删除功能,它可能对您有所帮助,

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

【讨论】:

  • 你需要在运行之前包含jquery文件或者你可以使用CDN
【解决方案5】:

* 如果您使用的是 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多