使用ajax/jquery/php发送单个输入 [英] Send single input with ajax/jquery/php

查看:49
本文介绍了使用ajax/jquery/php发送单个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery/AJAX的新手. 我正在尝试使用jquery/ajax/php发送单个输入.

I'm new to jQuery / AJAX. I'm trying to send single input with jquery/ajax/php.

在线示例

LIVE EXAMPLE

但是,按提交后没有任何反应,我的错误在哪里? 任何帮助表示赞赏.

But, after pressing submit nothing is happening, where is my error? Any help much appreciated.

HTML:

<form action="submit.php">
    <input id="number" name="number" type="text" />
    <input id="submit" name="submit" type="submit" />
</form>

JQUERY/AJAX:

JQUERY / AJAX:

$(document).ready(function(e) {
    $('input#submit').click(function() {
        var number = $('input[name=number]');
        var data = 'number=' + number.val();

        $.ajax({
            url: "submit.php",
            type: "GET",
            data: data,
            cache: false,
            success: function(html) {
                if (html == 1) {
                    alert('wyslane');
                }
                else {
                    alert('error');
                }
            }
        });
        return false;
    });
});

PHP:

<?php 
    $mailTo = 'email@gmail.com';
    $mailFrom = 'email@gmail.com';
    $subject = 'Call Back';
    $number = ($_GET['number']) ? $_GET['number'] : $_POST['number'];   
    mail($mailTo, $subject, $number, "From: ".$mailFrom);
?>

推荐答案

HTML:

<form id=submit action="">
  <input id="number" name="number" type="text" />
  <input name="submit" type="submit" />
</form>

操作URL与您要通过AJAX提交数据无关.将submit id添加到表单中并覆盖默认的提交行为,而不是覆盖Submit按钮的onclick处理程序.我将在JS部分中进行解释.

The action URL is irrelevant as you want to submit your data via AJAX. Add the submit id to the form and override the default submit behavior, instead of overriding the onclick handler of the submit button. I'll explain in the JS section.

JS:

var number = $('input[name="number"]');

行情丢失了.

$(document).ready(function(e) {
    $('#submit').submit(function() {
        var number = $('input[name=number]');
        var data = 'number=' + number.val();

        $.ajax({
            url: "submit.php",
            type: "GET",
            data: data,
            cache: false,
            success: function(html) {
                if (html == 1) {
                    alert('wyslane');
                }
                else {
                    alert('error');
                }
            }
        });
        return false;
    });
});

我不太了解您的成功回调,为什么您期望html应该等于1?

I don't really understand your success callback, why do you expect that html should be equal to 1?

这篇关于使用ajax/jquery/php发送单个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆