简体   繁体   English

$ .ajax function()在提交表单时不起作用

[英]$.ajax function() not working when form is being submitted

I am trying to write an ajax function such that when i click the submit button, name = "Add", it will call for a php file named "addtobucketlist.php" and run the functions in it. 我试图编写一个ajax函数,以便当我单击提交按钮时,名称=“添加”,它将调用名为“ addtobucketlist.php”的php文件并在其中运行函数。

This is the ajax function in my file called "script.js" which is not working, and I have no idea why. 这是我名为“ script.js”的文件中的ajax函数,该函数无法正常工作,我也不知道为什么。 (I have verified that the hideshow('loading',1); at line 2 is working.) (我已验证第2行的hideshow('loading',1);正常工作。)

function Addtobucketlist(){
    hideshow('loading',1);     //line 2
    error(0);

$.ajax({
    type: "POST",
    url: "http://utourpia.me/php/addtobucketlist.php",      
    data: $('#addBucket').serialize(),
    dataType: "json",
    success: function(msg){

        if(parseInt(msg.status)==1)
        {
            //hide the form
            $('.form').fadeOut('slow');                 

            //show the success message
            $('.done').fadeIn('slow');
        }
        else if(parseInt(msg.status)==0)
        {
            error(1,msg.txt);
        }

        hideshow('loading',0);
    }
});

}

Below is my header.php 以下是我的header.php

$(document).ready(function(){

        $('#addBucket').submit(function(e) {
            Addtobucketlist();
            e.preventDefault(); 
        }); 
    });

Below is my form, this is being written such that when ajax is run, the form will be hidden and div class done will be shown. 下面是我的表单,正在编写该代码,以便在运行ajax时将隐藏该表单,并显示完成的div类。

echo '<div class=form>';
echo '<form id=addBucket action="http://utourpia.me/php/addtobucketlist.php" method=post>';
echo '<input type="submit" name="add" value="Add" /><img id=loading src="../../images/loading.gif" alt="working.." />';
echo '</form>';
echo '</div>';
echo '<div class="done"><p>Added successfully! <br /></p>';
echo '</div>';

below is addtobucketlist.php: 以下是addtobucketlist.php:

if (isset($_POST['Add'])) 
 {
    add_to_bucket_list($location_id, $username);
    die(msg(1,"<p>Added!</p>"));
}
else
{
var_dump($location_id);
var_dump($username);
}


function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

any help would be appreciated! 任何帮助,将不胜感激!

Try using 尝试使用

if (!empty($_POST['Add'])) 
{

instead of isset. 而不是isset。 Add the 添加

error: function( jqXHR, textStatus, errorThrown )

function to your ajax call to see what the error being thrown is. 函数对ajax进行调用,以查看所引发的错误是什么。

You use a capital 'A' in your php _POST variable 'Add', and in your html form it is a lowercase a. 您在php _POST变量“添加”中使用大写字母“ A”,并且在html形式中使用小写字母a。

The text you're outputting from your php script might not be identified as proper json. 您从php脚本输出的文本可能未标识为正确的json。 Use the php function json_encode() on an array of output values instead. 在输出值数组上使用php函数json_encode()代替。

action is not required as you are doing it via ajax post 通过ajax post进行操作时不需要执行操作
remove action="http://utourpia.me/php/addtobucketlist.php 删除action="http://utourpia.me/php/addtobucketlist.php

remove msg.status checks from $.ajax as it is only going to execute on success $.ajax删除msg.status检查,因为它将仅在成功时执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM