使用php和jquery ajax提交表单后,无法将数据插入数据库 [英] Unable to insert data into database after form submit using php and jquery ajax

查看:97
本文介绍了使用php和jquery ajax提交表单后,无法将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在提交表单后在数据库中插入数据,并希望将提交按钮的内容更改为已保存.我可以在提交表单后更改提交"按钮的内容,但将空白存储在数据库中.我的表单类似于这样.

I'm trying to insert data in database after submitting form and wants to change the content of the submit button to saved. I'm able to change the contents of submit button afer submitting form but stores blank spaces in database. My form is similar to like this.

html表单


<form id="yourForm">                                   
 <input type="text" name="building_length_in" id="building_length_in" placeholder="इन्च"class="form-control input-sm" value="" required>
 <input type="text" name="building_breadth_ft" id="building_breadth_ft" placeholder="फीट" class="form-control input-sm" value="" required>                                                                              

 <input class="submitBtn" type="submit" value="Submit"/>
   </form>

js


$(document).ready(function(){
    $('#yourForm').submit(function(event){
        $('.submitBtn').attr('value','Saved');

        setTimeout(function(){
            $('.submitBtn').attr('value','OK');
        }, 3000);

        var stuff = $('#yourForm').serialize();

        jQuery.ajax({
            type: 'POST',
            url: 'application_save.php', 
            data:{
                'stuff':stuff,                
            }
            ,success: function( response ){
              console.log(response);debugger;
            }
        });

        //Prevents form submission
        return false;        
    });
});

application_save.php

application_save.php


    <?php 
    require_once "conn.php";
    $building_length_in=$building_breadth_ft="";
    if($_SERVER["REQUEST_METHOD"] == "POST"){    
        $building_length_in = trim($_POST["building_length_in"]);
        $building_breadth_ft = trim($_POST["building_breadth_ft"]);  
        mysqli_set_charset('utf8');
        $sql = "INSERT INTO bps_registration_application 
                        (building_length_in,building_breadth_ft) 
                        VALUES (?, ?)";   

        if($stmt = mysqli_prepare($conn, $sql)){
            mysqli_stmt_bind_param($stmt, "ss",
                                    $building_length_in,
                                    $building_breadth_ft);      
            $building_length_in= $building_length_in;
            $building_breadth_ft=$building_breadth_ft;      
            if(mysqli_stmt_execute($stmt)){
                if(!empty($reg_id)){
                    $success = "Submitted form successfully .";     
                    header("location: registration_detail.php?success=$success&id=".$reg_id);
                    exit();
                } else {
                    header("location: registration_detail.php");
                    exit();   
                }     
            }else{
                echo "Something went wrong. Please try again later.";
            }
        }
        echo $success;
        mysqli_close($conn);
    }    
    ?>

console.log(stuff)包含以下数据.

console.log(stuff) contains following data .

stuff = "reg_id=74&registration_date=%E0%A5%A8%E0%A5%A6%E0%A5%AD%E0%A5%AC-%E0%A5%AA-%E0%A5%A7%E0%A5%A6&building_use_id=residential&building_category=a&building_structure_category_id=rcc&building_length_ft=4&building_length_in=4&building_breadth_ft=4&building_breadth_in=4&building_height_ft=4&building_height_in=4&building_storey=4&building_area=46666"

<br />
<b>Notice</b>:  Undefined index: reg_id in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>8</b><br />
<br />
<b>Notice</b>:  Undefined index: registration_date in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>9</b><br />
<br />
<b>Notice</b>:  Undefined index: building_use_id in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>10</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>10</b><br />
<br />
<b>Notice</b>:  Undefined index: building_category in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>11</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>11</b><br />
<br />
<b>Notice</b>:  Undefined index: building_structure_category_id in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  Undefined index: building_length_ft in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>13</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>13</b><br />
<br />
<b>Notice</b>:  Undefined index: building_length_in in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>14</b><br />
<br />
<b>Notice</b>:  Undefined index: building_breadth_ft in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>15</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>15</b><br />
<br />
<b>Notice</b>:  Undefined index: building_breadth_in in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>16</b><br />
<br />
<b>Notice</b>:  Undefined index: building_height_ft in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>:  Undefined index: building_height_in in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>18</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>18</b><br />
<br />
<b>Notice</b>:  Undefined index: building_storey in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>19</b><br />
<br />
<b>Notice</b>:  Undefined index: building_area in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  mysqli_set_charset() expects exactly 2 parameters, 1 given in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>21</b><br />
<br />
<b>Notice</b>:  Undefined index: reg_id in <b>C:\xampp\htdocs\bps\api\application_save.php</b> on line <b>26</b><br />
{"msg":"Something went wrong. Please try again later.","reg_id":0}

推荐答案

application_save.php中更改以下代码:

if($_SERVER["REQUEST_METHOD"] == "POST"){    
  // $building_length_in = trim($_POST["building_length_in"]);
  // $building_breadth_ft = trim($_POST["building_breadth_ft"]);      
  $temp = explode('&', $_POST['stuff']);
  $keys = array_keys($temp);
  for($i=0;$i<count($keys);$i++) {
    $afterstr = substr($temp[$i], strpos($temp[$i], '=') + 1);
    $temp[strstr($temp[$i],'=',true)] = $afterstr;
    unset($temp[$i]);
  }

  $t = extract($temp); // here you can get all the posted variables like $reg_id, $registration_date, $building_use_id and so on.. 
  mysqli_set_charset('utf8');
  $sql = "INSERT INTO bps_registration_application (building_length_in,building_breadth_ft) VALUES (?, ?)";   
  if($stmt = mysqli_prepare($conn, $sql)){
    mysqli_stmt_bind_param($stmt, "ss", $building_length_in, $building_breadth_ft);
    $building_length_in = $building_length_in;
    $building_breadth_ft = $building_breadth_ft;
    if(mysqli_stmt_execute($stmt)){
      if(!empty($reg_id)){
        $data['msg'] = "Submitted form successfully .";
        $data['reg_id'] = $reg_id;
      } else {
        $data['msg'] = "Form submission could not be completed, please try again!";
        $data['reg_id'] = 0;
      }
    } else {
      $data['msg'] = "Something went wrong. Please try again later.";
      $data['reg_id'] = 0;
    }
  }
  echo json_encode($data);
}

AJAX:

AJAX:

$(document).ready(function(){
  $('#yourForm').submit(function(event){
      $('.submitBtn').attr('value','Saved');
      setTimeout(function(){
          $('.submitBtn').attr('value','OK');
      }, 3000);
      var stuff = $('#yourForm').serialize();
      jQuery.ajax({
          type: 'POST',
          url: 'application_save.php',
          data:{ stuff:stuff },
          dataType: "JSON",
          success: function( response ){
            console.log(res);debugger;
            var res = JSON.stringify(response);
            if(res.reg_id != 0) {
              window.location = 'registration_detail.php?success='+ res.msg +'&id=' + res.reg_id;
            } else {
              window.location = 'registration_detail.php?success='+ res.msg;
            }
          }
      });

      //Prevents form submission
      return false;
  });
});

这篇关于使用php和jquery ajax提交表单后,无法将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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