Javascript 表单发布后的PHP重定向
我在我的网站上做了一个下载按钮,打开一个请求密码的模式,然后如果密码正确,应该带用户去真正的下载。这是我的javascript:Javascript 表单发布后的PHP重定向,javascript,php,html,Javascript,Php,Html,我在我的网站上做了一个下载按钮,打开一个请求密码的模式,然后如果密码正确,应该带用户去真正的下载。这是我的javascript: function submit() { var postData = $("#passwd").serialize(); //alert(postData); $.ajax({ type: "POST", url: "http://www.redcraft.it/submit.php", dat
function submit() {
var postData = $("#passwd").serialize();
//alert(postData);
$.ajax({
type: "POST",
url: "http://www.redcraft.it/submit.php",
data: postData,
success: function(redirect) {
alert('Submitted')
}/*,
error: function() {
alert('Failure');
}*/
});
}
这是我的php:
<?php
function redirect() {
$data = $_POST["postData"];
if($data == is_null()) {
echo "Error";
} else {
echo "<script>window.open('http://www.google.com/" + $data + "', '_self')</script>";
}
}
?>
只需在html中添加一个div
<div id="outputredirect"></div>
javascript代码应重定向到php脚本返回的url:
function submit() {
var postData = $("#passwd").serialize();
//alert(postData);
$.ajax({
type: "POST",
url: "http://www.redcraft.it/submit.php",
data: postData,
success: function(redirect) {
location.href(redirect);
}/*,
error: function() {
alert('Failure');
}*/
});
}
你想做什么?如果成功,你不能从JSU调用php函数,Ajax的目的是在不离开当前页面的情况下发出HTTP请求。既然你想离开当前页面:为什么要使用Ajax?首先是因为只有在密码正确的情况下它才能重定向,其次是因为我知道这是我找到的唯一方法,正如我说的,我是web开发新手。好的,通过这种方式,我可以重定向,但php中的密码值为Null。请提供您的html以了解如何指定字段名。只需像这样更改您的数据--data:{“postData”:jQuery('passwd')。val()},您在对话框中不使用完整的html表单。因此,序列化不起作用,只需直接输入值并传入ajax数据即可
if(redirect == "Error"){
alert(redirect);
} else {
jQuery('#outputredirect').html(redirect);
}
function submit() {
var postData = $("#passwd").serialize();
//alert(postData);
$.ajax({
type: "POST",
url: "http://www.redcraft.it/submit.php",
data: postData,
success: function(redirect) {
location.href(redirect);
}/*,
error: function() {
alert('Failure');
}*/
});
}