使用AJAX将多个js变量解析为php [英] parse mutiple js variables to php using AJAX

查看:80
本文介绍了使用AJAX将多个js变量解析为php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个变量保存到数据库,但是大多数变量是使用java-script/jquery计算的.所以我已经使用.js/ajax和.php来这样做,但是我在执行此操作时遇到了错误.这是我的代码示例-

I want to save multiple variables to database, but most of the variable are calculated using java-script/jquery. so i have used .js/ajax and .php to do so but i am getting error on doing this. here is my code example-

JS/Jquery-

JS/Jquery-

$(function(){
  $("#btn").on('click', function() {
    var name =  $("#myname").val();
    var yoursex = $("input[name='gender']:checked").val();
    var date = new Date($('#dob').val());
    var c_age = calc_age();
    var social_age = calcSocialAge();

$.post("insert_data.php", {
    name: name,
    sex: yoursex,
    dob: date,
    crono_age: c_age,
    social_age: social_age
},
      function(data, status){
          alert("Data: " + data + "\nStatus: " + status);
      });
});
});

insert_data.php

insert_data.php

<? php  
include_once('connect-mysql.php') ;

      $name = $_post['name'];
      $sex = $_post['sex'];
      $dob = $_post['dob'];
      $c_age = $_post['crono_age'];
      $s_age = $_post['social_age'];



     $sql = " INSERT INTO `patient_info` (`pt_id`, `pt_name`, `pt_gender`, `Pt_dob`, `pt_C_age`, `pt_s_age`)
    VALUES ('', $name,$sex, $dob, $c_age, $c_age, $s_age );";

     ?>

当我应用此代码但出现如下错误时-

when i am applying this code but getting the error like below -

数据:已成功连接
通知:未定义变量:_post在 4至8行的 E:\ xampp \ htdocs \ insert_data.php

状态:成功

我对AJAX和PHP还是很陌生,请帮助解决该问题.

Data: Connected successfully
Notice: Undefined variable: _post in E:\xampp\htdocs\insert_data.php on line 4 to 8

Status: success

I am quite new to AJAX and PHP please help to resolve the issue.

推荐答案

$_post不是

$_post is not $_POST Change all to $_POST, and also check value

  $name = isset($_POST['name'])?$_POST['name']:'';
  $sex = isset($_POST['sex'])?$_POST['sex']:'';
  $dob = isset($_POST['dob'])?$_POST['dob']:'';
  $c_age = isset($_POST['crono_age'])?$_POST['crono_age']:'';
  $s_age = isset($_POST['social_age'])?$_POST['social_age']:'';

date是对象,不是像Y-m-dd-m-Y所期望的任何字符串值,类似于Date 2017-12-23T07:18:08.909Z在这里

And date is object not any string value as you are expecting like Y-m-d or d-m-Y, it is something like Date 2017-12-23T07:18:08.909Z here

var date = new Date($('#dob').val());//be sure about date value

您没有使用quotes在查询中插入字符串类型值,但是我建议使用mysqli或pdo插入.这里是一些有关mysqli的链接:

You are not using quotes to insert string type value in your query, But I suggest to insert use mysqli or pdo. Here are some link to learn about mysqli:

mysqli_prepare

mysqli_stmt_bind_param

MySQLi中的预备语句

这篇关于使用AJAX将多个js变量解析为php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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