Insert data through ajax into mysql database Insert data through ajax into mysql database ajax ajax

Insert data through ajax into mysql database


Try this:

  $(document).on('click','#save',function(e) {  var data = $("#form-search").serialize();  $.ajax({         data: data,         type: "post",         url: "insertmail.php",         success: function(data){              alert("Data Save: " + data);         }}); });

and in insertmail.php:

<?php if(isset($_REQUEST)){        mysql_connect("localhost","root","");mysql_select_db("eciticket_db");error_reporting(E_ALL && ~E_NOTICE);$email=$_POST['email'];$sql="INSERT INTO newsletter_email(email) VALUES ('$email')";$result=mysql_query($sql);if($result){echo "You have been successfully subscribed.";}}?>

Don't use mysql_ it's deprecated.

another method:

Actually if your problem is null value inserted into the database then try this and here no need of ajax.

<?phpif($_POST['email']!=""){    mysql_connect("localhost","root","");    mysql_select_db("eciticket_db");    error_reporting(E_ALL && ~E_NOTICE);    $email=$_POST['email'];    $sql="INSERT INTO newsletter_email(email) VALUES ('$email')";    $result=mysql_query($sql);    if($result){    //echo "You have been successfully subscribed.";              setcookie("msg","You have been successfully subscribed.",time()+5,"/");              header("location:yourphppage.php");    }     if(!$sql)    die(mysql_error());    mysql_close();}?>    <?php if(isset($_COOKIE['msg'])){?>       <span><?php echo $_COOKIE['msg'];setcookie("msg","",time()-5,"/");?></span>     <?php }?><form id="form-search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">    <span><span class="style2">Enter you email here</span>:</span>    <input name="email" type="email" id="email" required/>    <input type="submit" value="subscribe" class="submit"/></form>


The ajax is going to be a javascript snippet that passes information to a small php file that does what you want. So in your page, instead of all that php, you want a little javascript, preferable jquery:

function fun(){    $.get('\addEmail.php', {email : $(this).val()}, function(data) {        //here you would write the "you ve been successfully subscribed" div    });}

also you input would have to be:

<input type="button" value="subscribe" class="submit" onclick="fun();" />

last the file addEmail.php should look something like:

mysql_connect("localhost","root","");mysql_select_db("eciticket_db");error_reporting(E_ALL && ~E_NOTICE);$email=mysql_real_escape_string($_GET['email']);$sql="INSERT INTO newsletter_email(email) VALUES ('$email')";$result=mysql_query($sql);if($result){echo "You have been successfully subscribed.";} if(!$sql)die(mysql_error());mysql_close();

Also sergey is right, you should use mysqli. That's not everything, but enough to get you started.


Why use normal jquery ajax feature. Why not use jquery ajax form plugin, which post the form data by ajax to the form action link.

Check it here:

http://malsup.com/jquery/form/#getting-started

It is very easy to use and support several data formats including json, html xml etc. Checkout the example and you will find it very easy to use.

Thank you