jQuery Ajax 不从外部 PHP 返回变量

发布于 2024-12-06 15:38:38 字数 2007 浏览 0 评论 0原文

我有这段代码,它从表单中获取信息并将其发送到 form.php 进行处理。 form.php 回显应该输入到#success 的变量(全名:$fullname 电子邮件:$email 链接:$link 说明:$instr)。我的代码在 Firefox 和 Chrome 中有两种不同的行为方式。

在 Firefox 中,我被发送到 form.php,它会正确显示输出,但显然我不应该被发送到那里,我应该留在我的主页上并在 #success 中看到该输出。基本上,ajax 不起作用。

在 Chrome 中,ajax 确实可以工作,但只能将全名:电子邮件:链接:说明:拉入#success。本质上,jQuery 并不是通过 POST 传递变量。

mainpage.php:

<script type="text/javascript">
$(document).ready(function() {
    var data = $(form).serialize();
    $('#form').submit(function(event) {   
            $.ajax({
            url: '../wp-content/themes/MC/form.php',
            type: 'POST',
            data: data,
            success: function(result) {
                $('#success').html(result);
            }
            }); 
     event.preventDefault();
     });
})
</script>

<div id="exchange_inside">
<div id="formdiv">
<br>
<form method="post" id="form">
    Name / Nom:<input type="text" name="fullname" /><br />
    E-mail / Courriel:<input type="text" name="email" /><br />                          Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
    Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
    <input type="submit" value="Submit" />
</form>
</div>
</div>
<div id="success">
</div>

表单.php:

<?php
if (isset($_POST['fullname'])){
    $fullname = $_POST['fullname'];   
}else{
    echo "No name";
}

if (isset($_POST['email'])){
    $email = $_POST['email'];   
}else{
    echo "No email";
}

if (isset($_POST['link'])){
    $link = $_POST['link'];   
}else{
    echo "No link";
}

if (isset($_POST['instr'])){
    $instr = $_POST['instr'];   
}else{
    echo "No instructions";
}

echo "Name: " .$fullname. " E-mail: " .$email. " Link: " .$link. " Instructions: " .$instr;
?>

I have this code that takes info from a form and sends it to form.php where it is processed. form.php echoes the variables (Fullname: $fullname E-mail: $email Link: $link Instructions: $instr) which are supposed to be inputted to #success. My code behaves two different ways in firefox and chrome.

In Firefox, I am sent to form.php which displays the output properly but obviously I shouldn't be sent there, I should stay on my main page and see that output in #success. Basically, the ajax doesn't work.

In Chrome, the ajax does work but only pulls Fullname: Email: Link: Instructions: into #success. In essence, the jQuery is not passing the variables via POST.

mainpage.php:

<script type="text/javascript">
$(document).ready(function() {
    var data = $(form).serialize();
    $('#form').submit(function(event) {   
            $.ajax({
            url: '../wp-content/themes/MC/form.php',
            type: 'POST',
            data: data,
            success: function(result) {
                $('#success').html(result);
            }
            }); 
     event.preventDefault();
     });
})
</script>

<div id="exchange_inside">
<div id="formdiv">
<br>
<form method="post" id="form">
    Name / Nom:<input type="text" name="fullname" /><br />
    E-mail / Courriel:<input type="text" name="email" /><br />                          Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
    Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
    <input type="submit" value="Submit" />
</form>
</div>
</div>
<div id="success">
</div>

form.php:

<?php
if (isset($_POST['fullname'])){
    $fullname = $_POST['fullname'];   
}else{
    echo "No name";
}

if (isset($_POST['email'])){
    $email = $_POST['email'];   
}else{
    echo "No email";
}

if (isset($_POST['link'])){
    $link = $_POST['link'];   
}else{
    echo "No link";
}

if (isset($_POST['instr'])){
    $instr = $_POST['instr'];   
}else{
    echo "No instructions";
}

echo "Name: " .$fullname. " E-mail: " .$email. " Link: " .$link. " Instructions: " .$instr;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-12-13 15:38:38

尝试通过将 event.preventDefault(); 移至 ajax 调用之前稍微更改一下 jquery 并修复 var data = $("#form" ).serialize();

$(document).ready(function() {

    $('#form').submit(function(event) { 
        event.preventDefault();  
        var data = $("#form").serialize();
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
        }); 

 });
}); //semicolon here

$(document).ready 末尾添加分号以方便衡量。

如果 ajax 由于某种原因失败或在 ajax 调用中出现错误,则在 ajax 调用之前移动 event.preventDefault() 会阻止表单提交。 ajax 调用。主要是使用这种表单提交方法,您需要首先停止表单的默认行为。

Try changing up the jquery a bit by moving up event.preventDefault(); to before the ajax call and fix your var data = $("#form").serialize();:

$(document).ready(function() {

    $('#form').submit(function(event) { 
        event.preventDefault();  
        var data = $("#form").serialize();
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
        }); 

 });
}); //semicolon here

Put a semicolon on the end of $(document).ready for good measure.

Moving event.preventDefault() before the ajax call stops the form from submitting if the ajax fails for some reason or there is an error in the ajax call. Primarily with this method of form submit, you want to stop the default behavior of the form first.

蓝海似她心 2024-12-13 15:38:38

尝试去掉 $('#form').submit,它在不使用 ajax 的情况下提交表单,如下所示:

$(document).ready(function() {
    var data = $('#form').serialize(); //this selector also needed to be fixed 
    $.ajax({
            url: '../wp-content/themes/MC/form.php',
            type: 'POST',
            data: data,
            success: function(result) {
                $('#success').html(result);
            }
    }); 
});

编辑:
我误会了。尝试在按下提交后序列化数据,而不是在页面加载时序列化数据,以解决 Chrome 问题。

$('#form').submit(function(event) {   
        var data = $('#form').serialize(); //moved this line and fixed selector
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
        }); 
        event.preventDefault();
 });

您可能还想尝试将提交按钮更改为常规按钮并将 ajax 绑定到其 onclick 事件。这将避免在没有ajax的情况下提交表单的问题,并修复Firefox问题。

Try getting rid of the $('#form').submit, which is submitting the form without ajax, like this:

$(document).ready(function() {
    var data = $('#form').serialize(); //this selector also needed to be fixed 
    $.ajax({
            url: '../wp-content/themes/MC/form.php',
            type: 'POST',
            data: data,
            success: function(result) {
                $('#success').html(result);
            }
    }); 
});

Edit:
I misunderstood. Try serializing the data after submit has been pressed, rather than when the page loads, to fix the Chrome problem.

$('#form').submit(function(event) {   
        var data = $('#form').serialize(); //moved this line and fixed selector
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
        }); 
        event.preventDefault();
 });

You may also want to try changing the submit button to a regular button and binding the ajax to its onclick event. This would avoid problems with the form submitting without ajax, and fix the Firefox problem.

岁月流歌 2024-12-13 15:38:38
$(document).ready(function() {

    $("#form").submit(function(event) {   

         var data = $("#form").serialize();
            $.ajax({
            url: '/echo/json/',
            type: 'POST',
            data: data,
            success: function(data, status, xhr) {

                console.log(data);
                console.log(status);
                console.log(xhr);
                $('#success').html(result.responseText);
            }

            });

    event.preventDefault();
        return false;
     });
})
  1. 您的数据声明不在全局范围内。(我将其打包在正确的本地范围内)
  2. $(form).serialize() 不等于 $("#form").serialize()
  3. 编辑回您的url =)
  4. 使用 Jsfiddle 和 Firebug/Chrome 开发工具来测试您的代码!

希望有帮助

$(document).ready(function() {

    $("#form").submit(function(event) {   

         var data = $("#form").serialize();
            $.ajax({
            url: '/echo/json/',
            type: 'POST',
            data: data,
            success: function(data, status, xhr) {

                console.log(data);
                console.log(status);
                console.log(xhr);
                $('#success').html(result.responseText);
            }

            });

    event.preventDefault();
        return false;
     });
})
  1. Your declaration of data wasn't in the global scope.(i packed it in the right local scope)
  2. $(form).serialize() isn't equal to $("#form").serialize()
  3. Edit back in your url =)
  4. Use Jsfiddle and Firebug/Chrome Dev Tools to test your code!

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文