使用jQuery和PHP保存到没有表单的数据库

时间:2022-11-24 13:26:16

I'm trying to save some data to a database without the use of an html form and was wondering if anyone could help me as I'm no expert in PHP. So far I have got:

我正在尝试将一些数据保存到数据库而不使用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");
                }
            });
        });

There is no issue at the first stage as all my values are stored in the variables correctly. I just don't know in what format to send them across to save.php.

在第一阶段没有问题,因为我的所有值都正确地存储在变量中。我只是不知道以什么格式将它们发送到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();
  }
 }
?>

I'm not sure how to post this data to save.php and then how to process it to be sent to the database. I've also added a variable of current_date to send the current date to a field in the database.

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

Can anyone help me and fill in the blanks? Or maybe I'm going about this the wrong way?

任何人都可以帮助我并填补空白吗?或者也许我会以错误的方式解决这个问题?

2 个解决方案

#1


1  

Send your data in an object, like so:

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

// 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");
    }
});

Then, on the server side, you can access directly via $_POST superglobal:

然后,在服务器端,您可以直接通过$ _POST superglobal访问:

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

#2


1  

You can send all this data in the data parameter as given below:

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

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

$('。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: {summary_weight: summary_weight, summary_bmi:summary_bmi, summary_consumed:summary_consumed, summary_burned: summary_burned, summary_total:summary_total, user_id:user_id },
            success: function () {
                $('.success_message').html("success");
            }
        });
    });

And the, process it in save.php like this $summary_weight = $_POST['summary_weight']; and use it in the query to save it in database.

然后,在save.php中处理它,如$ summary_weight = $ _POST ['summary_weight'];并在查询中使用它将其保存在数据库中。

#1


1  

Send your data in an object, like so:

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

// 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");
    }
});

Then, on the server side, you can access directly via $_POST superglobal:

然后,在服务器端,您可以直接通过$ _POST superglobal访问:

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

#2


1  

You can send all this data in the data parameter as given below:

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

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

$('。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: {summary_weight: summary_weight, summary_bmi:summary_bmi, summary_consumed:summary_consumed, summary_burned: summary_burned, summary_total:summary_total, user_id:user_id },
            success: function () {
                $('.success_message').html("success");
            }
        });
    });

And the, process it in save.php like this $summary_weight = $_POST['summary_weight']; and use it in the query to save it in database.

然后,在save.php中处理它,如$ summary_weight = $ _POST ['summary_weight'];并在查询中使用它将其保存在数据库中。