jquery.ajax通过POST发出多个数据请求

时间:2015-11-20 03:30:38

标签: php jquery ajax jquery-plugins

我使用jquery.ajax使用php / mysql在表中插入字段。我似乎写了一个错误的代码和关于数据的错误语法:jquery.ajax中的firld 功能。有人可以为我提供正确的方法。 javascript如下:

var degree_name = $("#degree_name").val();alert("Pass 4"); //build a post data structure
        var degree_school = $("#degree_school").val(); //build a post data structure
        var area_of_specialisation_degree = $("#area_of_specialisation_degree").val(); //build a post data structure
        var date_of_school_passed = $("#date_of_school_passed").val(); //build a post data structure

    jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "response.php", //Where to make Ajax calls
        dataType:"text", // Data type, HTML, json etc.
        data:{'degree_school':degree_school,'degree_name':degree_name,'area_of_specialisation_degree':area_of_specialisation_degree,'date_of_school_passed':date_of_school_passed},//Form variables
        success:function(response){
            $("#responds").append(response);
            $("#contentText").val(''); //empty text field on successful
            $("#FormSubmit").show(); //show submit button
            $("#LoadingImage").hide(); //hide loading image

        },
        error:function (xhr, ajaxOptions, thrownError){
            $("#FormSubmit").show(); //show submit button
            $("#LoadingImage").hide(); //hide loading image
            alert(thrownError);
        }
        });

相应的 response.php 文件为:

<?php
//include db configuration file
require_once 'dbconfig.php';
echo "I am here in response";

if(isset($_POST["degree_name"]) && isset($_POST["degree_school"]) &&     isset($_POST["area_of_specialisation_degree"])
 && isset($_POST["date_of_school_passed"]) &&   strlen($_POST["area_of_specialisation_degree"])>0 &&    strlen($_POST["date_of_school_passed"])>0
&& strlen($_POST["degree_name"])>0 && strlen($_POST["degree_school"])>0 ) 
{   //check $_POST["content_txt"] is not empty
try {
   // create database connection
    $dbh = new PDO($dsn, $username, $password);
    echo "I am here inside try catch response";
    // create new user
    $insertDegreeNew = insertNewDegree();

    if($insertDegreeNew)
    {
      ....

等等。 请帮我识别我的错误。

0 个答案:

没有答案