0

I am working on project where i have to insert data into mysql database using jquery serialize function. All data is inserted well except data from select tag. I have spent two days but not able to understand where i am wrong. here is my jquery code.

<script>
    $(document).ready(function(){
    $("#addfields").click(function(e){
    e.preventDefault();
        $.ajax({
            url: "makeschedule.php",
            type: "post",
            data: $("#scheduleform").serialize(),
            success: function(d) {
                 alert(d);
            }
        }); 
    });
    });
</script>

<form method="post">
    <select class="form-control" id="fromtime" name="fromtime">
        <?php
            for($i=1;$i<=12;$i++)
                {                   
                echo "<option value='$i'>$i.00</option>";
                }
        ?>                  
    </select>

    <select class="form-control" id="totime" name="totime">
        <?php
            for($i=1;$i<=12;$i++)
                {                   
                echo "<option value='$i'>$i.00</option>";
                }
        ?>                  
    </select>
    <a href="#" class="" id="addfields" name="addfields"><img src="imgs/icons/add.png" height=35 width=35></a>
</form>

And PHP Code is :

<?php
if($_POST)
{
$userid=$_SESSION['s_userid'];
$userid=2;
$day=  mysqli_real_escape_string($_POST['day']);
$fromtime= mysqli_real_escape_string($_POST['fromtime']);

$time1=    mysqli_real_escape_string($_POST['am_pm1']);
$totime=  mysqli_real_escape_string($_POST['totime']);
$time2=  mysqli_real_escape_string($_POST['am_pm2']);

$qry="insert into a_yogischedule(id,userid,dayofweek,starttime,endtime,date) values(NULL,'$userid','2','$fromtime','$totime',now())";

$r=$dbconn->query($qry);
   if($r)
       echo "Success";
   else 
       echo "Fail";

}
?>

Thanks in advance.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Why are you using `mysqli_real_escape_string()` instead of `$dbconn->real_escape_string()`? – Machavity Aug 27 '15 at 15:17
  • Hello Sir, I do not know the correct syntax to use this. please tell me in detail and also my problem is all data is inserted into form expect select values.. :( –  Aug 27 '15 at 15:22
  • Also, you're calling `$("#scheduleform").serialize()` but nothing has the id `scheduleform` – Machavity Aug 27 '15 at 15:23
  • i can't believe ..i am such a stupid person.. the problem was with mysqli_real_escape_string(). I removed that methods and it work fine now. JUST SPOIL MY WHOLE WEEK ON THIS. :( –  Aug 27 '15 at 15:28
  • Sorry.I just typed form tag manually in hurry. actually form has id attribute in my code. thank you all for helping me.. –  Aug 27 '15 at 15:29

0 Answers0