How to Auto Save Selection in ComboBox into MYSQL in PHP without submit button?

对着背影说爱祢 提交于 2019-12-13 05:29:07

问题


I have no experience with jquery and ajax, so far I just looking for source and edit paste the code into my coding. Now I try to look for tutorial autosave combobox selection but i fail to find it.Can someone help me? I only done with MYSQL display record, but I do not know how to auto update combobox selection nito MYSQL using jquery. Example, I want to select booking status, when i choose approve from combobox, it will automatically save into MYSQL without click button submit.

<?php

include('config.php');

$per_page = 9; 

if($_GET)
{
$page=$_GET['page'];
}



//get table contents
$start = ($page-1)*$per_page;
$sql = "SELECT bookingID,eventinfo.eventTitle,boothAlias,testbook.bstatus,date, testbook.username, customer.companyName, customer.contactName from eventinfo, testbook, customer where testbook.username=customer.username AND testbook.eventID=eventinfo.eventID order by date desc limit $start,$per_page";
$rsd = mysql_query($sql);
?>

<form method="post" name="form">
<table width="800px">

    <?php
    //Print the contents

    while($row = mysql_fetch_array($rsd))
    {

        $id=$row['companyName'];
        $contactName=$row['contactName'];
        $eventTitle=$row['eventTitle'];
        $date=$row['date'];
        $status=$row['bstatus'];
        $booth=$row['boothAlias']

    ?>
    <tr><td style="color:#B2b2b2; padding-left:4px"><?php echo $id; ?></td><td><?php echo $contactName; ?></td>
    <td><?php echo $eventTitle; ?></td><td><?php echo $booth; ?></td><td><?php echo $date; ?></td><td><select name='status' id='status'>
        <option value='-1'>--Select--</option>
    <option value='0'>Approve</option>
    <option value='1'>Reject</option>
    <option value='2'>Pending</option>
    </select></td>
    </tr>
    <?php
    } 
    ?>
</table>
</form>

image


回答1:


do this

<select name='status' id='status'>
        <option value=''>--Select--</option>
        <option value='0'>Approve</option>
        <option value='1'>Reject</option>
        <option value='2'>Pending</option>
</select>
<div id="autosavenotify"></div>

<script>
$(document).ready(function(){
$('select').live('change',function () {
        var statusVal = $(this).val();
        alert(statusVal);
        $.ajax({
                 type: "POST",
                 url: "saveStatus.php",
                 data: {statusType : statusVal },
                 success: function(msg) {
                     $('#autosavenotify').text(msg);
                 }
      })
  });
});
</script>

on saveStatus.php do your mysql update

<?php

 $st=$_POST['statusType'];
 $qry =" UPDATE tableName SET `tablefield`=$st .. ";
 $done = mysql_query($qry);
 if($done)
 {
   echo "Saved Successfully";
 }
?>



回答2:


my first guess would be to use onChange event in select element, eg. <select name='status' id='status' onChange='updateMySQL();'>

in updateMySQL() you could call external script to save data into database. I'm not sure hot to achieve it though, it's just a guess. good luck in finding solution!



来源:https://stackoverflow.com/questions/7682713/how-to-auto-save-selection-in-combobox-into-mysql-in-php-without-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!