Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery和PHP不使用表单保存到数据库_Php_Jquery_Mysql - Fatal编程技术网

使用jQuery和PHP不使用表单保存到数据库

使用jQuery和PHP不使用表单保存到数据库,php,jquery,mysql,Php,Jquery,Mysql,我试图在不使用html表单的情况下将一些数据保存到数据库中,不知道是否有人可以帮助我,因为我不是PHP专家。到目前为止,我得到了: JQuery $('.summary').on('click', '#btn_save', function () { var summary_weight = $('#summary_weight').text(); var summary_bmi = $('#summary_bmi').text();

我试图在不使用html表单的情况下将一些数据保存到数据库中,不知道是否有人可以帮助我,因为我不是PHP专家。到目前为止,我得到了:

JQuery

$('.summary').on('click', '#btn_save', function () {

            var summary_weight = $('#summary_weight').text();
            var summary_bmi = $('#summary_bmi').text();
            var summary_consumed = $('#summary_consumed').text();
            var summary_burned = $('#summary_burned').text();
            var summary_total = $('#summary_total').text();
            var user_id = $('#user_id').text();

            //All values stored correctly

            $.ajax({
                type: "POST",
                url: "save.php",
                data: //Data to send,
                success: function () {
                    $('.success_message').html("success");
                }
            });
        });
在第一阶段没有问题,因为我所有的值都正确地存储在变量中。我只是不知道用什么格式将它们发送到save.php

save.php

<?php
require_once 'dbconfig.php';
//Connects to database

if($_POST)
 {
     //Not sure what to post here

$current_date = date('Y-m-d');

  try{

   $stmt = $db_con->prepare("INSERT INTO entry(user_id, date, weight, bmi, calories_consumed, calories_burned, calorific_deficit) VALUES(:user, :date, :weight, :bmi, :consumed, :burned, :deficit)");

   $stmt->bindParam(":user", $user_id);
   $stmt->bindParam(":date", $current_date);
   $stmt->bindParam(":weight", $summary_weight);
   $stmt->bindParam(":bmi", $summary_bmi);
   $stmt->bindParam(":consumed", $summary_consumed);
   $stmt->bindParam(":burned", $summary_burned);
   $stmt->bindParam(":deficit", $summary_total);

   if($stmt->execute())
   {
    echo "Successfully Added";
   }
   else{
    echo "Query Problem";
   } 
  }

  catch(PDOException $e){
   echo $e->getMessage();
  }
 }
?>

我不知道如何将此数据发布到save.php,然后如何处理它以发送到数据库。我还添加了一个current_date变量,将当前日期发送到数据库中的一个字段


谁能帮我填空吗?或者可能我做得不对?

用对象发送数据,如下所示:

// Declare data as an empty object
var data = {};
// Assemble the properties of the data object
data.summary_weight = $('#summary_weight').text();
data.summary_bmi = $('#summary_bmi').text();
data.summary_consumed = $('#summary_consumed').text();
data.summary_burned = $('#summary_burned').text();
data.summary_total = $('#summary_total').text();
data.user_id = $('#user_id').text();

$.ajax({
    type: "POST",
    url: "save.php",
    // pass the data object in to the data property here
    data: data,
    success: function () {
        $('.success_message').html("success");
    }
});
然后,在服务器端,您可以通过
$\u POST
superglobal直接访问:

$summary_weight = $_POST['summary_weight'];
$summary_bmi = $_POST['summary_bmi'];
// etc...

在对象中发送数据,如下所示:

// Declare data as an empty object
var data = {};
// Assemble the properties of the data object
data.summary_weight = $('#summary_weight').text();
data.summary_bmi = $('#summary_bmi').text();
data.summary_consumed = $('#summary_consumed').text();
data.summary_burned = $('#summary_burned').text();
data.summary_total = $('#summary_total').text();
data.user_id = $('#user_id').text();

$.ajax({
    type: "POST",
    url: "save.php",
    // pass the data object in to the data property here
    data: data,
    success: function () {
        $('.success_message').html("success");
    }
});
然后,在服务器端,您可以通过
$\u POST
superglobal直接访问:

$summary_weight = $_POST['summary_weight'];
$summary_bmi = $_POST['summary_bmi'];
// etc...

您可以在数据参数中发送所有这些数据,如下所示:

$('.summary')。在('click','btn_save',函数(){

然后,在save.php中像这样处理它 $summary\u weight=$\u POST['summary\u weight'];
并在查询中使用它将其保存到数据库中。

您可以在数据参数中发送所有这些数据,如下所示:

$('.summary')。在('click','btn_save',函数(){

然后,在save.php中像这样处理它 $summary\u weight=$\u POST['summary\u weight'];
并在查询中使用它将其保存到数据库中。

您不必转换服务器端的成员。jQuery将它们作为多个单独的参数发送。您不必转换服务器端的成员。jQuery将它们作为多个单独的参数发送。