My Jquery code for clicking the button in the form is:
$('#save1').on('click',function()
{
$.post('n.php',{'name': $('name').val(),'rollno': $('rollno').val(), 'mobile': $('mobile').val(), 'fblink': $('fblink').val(), 'linkedin': $('linkedin').val(),'skypelink': $('skypelink').val()},function(data) {
if(data=='success') {
$('#myModal').modal('show');
}
});
return false;
});
<form class="form-horizontal" action='n.php' method="POST">
Here the data is not getting posted though the alert box is popping
My php file n.php is:
<?php
$n=$_POST['name'];
$r=$_POST['rollno'];
$m=$_POST['mobile'];
$f=$_POST['fblink'];
$l=$_POST['linkedin'];
$s=$_POST['skypelink'];
$mysqli=new mysqli('localhost','root','password','user_details');
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$q=$mysqli->query("insert into store(Name,RollNo,MobileNo,FacebookLink,LinkedinLink,SkypeLink) values('$n','$r','$m','$f','$l','$s')");
$response='success';
echo $response;
?>
But when I remove return false from the click event function the data is stored in the database. Please help me in correcting the code so that when the user clicks the button the alert box is popped and the form values are stored in the database
Change Your Jquery code
from
$('#save1').on('click',function()
{
$.post('n.php',{'name': $('name').val(),'rollno': $('rollno').val(), 'mobile': $('mobile').val(), 'fblink': $('fblink').val(), 'linkedin': $('linkedin').val(),'skypelink': $('skypelink').val()},function(data) {
if(data=='success') {
$('#myModal').modal('show');
}
});
return false;
});
to
$('#save1').on('click',function()
{
$.post('n.php',{'name': $('#name').val(),'rollno': $('#rollno').val(), 'mobile': $('#mobile').val(), 'fblink': $('#fblink').val(), 'linkedin': $('#linkedin').val(),'skypelink': $('#skypelink').val()},function(data) {
if(data=='success') {
$('#myModal').modal('show');
}
});
return false;
});
always
use a # for getting element by id
use a . for getting element by class
Just as a note, you are widely leaving your application open for MySQL injection with the way you are handling your data.
You should look into PDO, and creating prepared statements for Database manipulation.