我有2个php文件。其中一个具有变量值,另一个使用这些值。我正在使用发布将值从一个文件传递到另一个文件。
<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>
但它并没有像我希望的那样工作。 只有在点击“确定”时它才会重定向。它不会在任何地方重定向。
答案 0 :(得分: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>
答案 1 :(得分:0)
使用onclick buttone时使用return参数:
</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;
}
}
答案 2 :(得分:0)
在第一个文件正在运行时,请检查此文件。
<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>
答案 3 :(得分:0)
我将此用于删除功能,它可能对您有所帮助,
<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>
答案 4 :(得分:0)
*如果您使用的是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>