Reputation: 127
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
<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®istration_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}
Upvotes: 0
Views: 422
Reputation: 4501
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;
});
});
Upvotes: 1
Reputation: 171700
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,
Upvotes: 2