0

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. enter image description here

html form


<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


    <?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) 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"

enter image description here

<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}

enter image description here

13
  • what's the error you are facing? Commented Aug 21, 2019 at 11:43
  • Did you check if stuff contains what you expect? What does the server recieve in $_POST? Did you try and execute the sql in $sql directly in the database and see if that behaves the same? Commented Aug 21, 2019 at 11:43
  • 2
    in application_save.php file where is $reg_id declared? Commented Aug 21, 2019 at 11:45
  • 2
    while your using a ajax. why are you triggering a header redirection? Commented Aug 21, 2019 at 11:45
  • 2
    Get rid of stuff property in data and just pass the serialized string .... data:stuff, Commented Aug 21, 2019 at 11:47

3 Answers 3

2

You are sending the serialized form data as a value of another property when all you want to send is the serialized string

That would make everything be inside $_POST['stuff'] and you would need to manually decode it yourself

Change

data:{
  'stuff':stuff,                
}

To

data: stuff,
Sign up to request clarification or add additional context in comments.

2 Comments

Is it also worth mentioning the use of header redirects in a AJAX API and the complete lack of a variable called $reg_id until it is tested ?
The form contains $_POST['reg_id'] as well as other input fields. I just include two input fields in question. @RiggsFolly
1

Change your below code in 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:

$(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;
  });
});

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
0

Change:

data:{
    'stuff':stuff,                
}

To:

data:{
    stuff:stuff            
}

5 Comments

Welcome to SO. You should add some explanation.
Quotes are irrelevant on javascript object property names
I have added more images of data that i got . Please help me. @charlietfl
@prabina I would like to know from you that my answer solved your problem or not?
No, it didn't solve my problem. I have solve it by another method. @Mac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.