javascript - Autosave from php to mysql without page change or submit button - Stack Overflow

Basically I have a list of data that is shown via a foreach statement from a table in my database. I wa

Basically I have a list of data that is shown via a foreach statement from a table in my database. I want a dropdown list next to each that has some values in them that need to be saved to a field in my table, however I want to autosave the value in the dropdown list to the field as soon as the value in it is changed. Can anyone point me to a tutorial or something that might help?

I am using php and mysql to create the system, but will happily use JavaScript if required

I have had a look at this: dynamic Drive Autosave.htm which is similar to what i want, however i need it to actually store that data in my database not temporary storage.

Any Guidance appreciated,

Ian


BIG EDIT

So, thankyou for the replys but I have no clue about ajax call.....I found this:How to Auto Save Selection in ComboBox into MYSQL in PHP without submit button?.

can i get it to work?

<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>


<?php foreach ( $results['jobs'] as $job ) { ?>

          <td width="25%"><?php echo $job->job_id</td> 
          <td>
          <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>
          </td>
        </tr>
    <?php } ?>

and on the page saveStatus.php:

<?php
if (isset($_POST['statusType'])) {

$con=mysql_connect("localhost","username","mypass","rocketdb3");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("jobs", $con);

$st=$_POST['statusType'];
    $query = "UPDATE jobs SET status=$st WHERE job_id=$id";
    $resource = mysql_query($query) 
        or die (mysql_error());

}
?>

Basically I have a list of data that is shown via a foreach statement from a table in my database. I want a dropdown list next to each that has some values in them that need to be saved to a field in my table, however I want to autosave the value in the dropdown list to the field as soon as the value in it is changed. Can anyone point me to a tutorial or something that might help?

I am using php and mysql to create the system, but will happily use JavaScript if required

I have had a look at this: dynamic Drive Autosavehttp://www.dynamicdrive./dynamicindex16/autosaveform.htm which is similar to what i want, however i need it to actually store that data in my database not temporary storage.

Any Guidance appreciated,

Ian


BIG EDIT

So, thankyou for the replys but I have no clue about ajax call.....I found this:How to Auto Save Selection in ComboBox into MYSQL in PHP without submit button?.

can i get it to work?

<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>


<?php foreach ( $results['jobs'] as $job ) { ?>

          <td width="25%"><?php echo $job->job_id</td> 
          <td>
          <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>
          </td>
        </tr>
    <?php } ?>

and on the page saveStatus.php:

<?php
if (isset($_POST['statusType'])) {

$con=mysql_connect("localhost","username","mypass","rocketdb3");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("jobs", $con);

$st=$_POST['statusType'];
    $query = "UPDATE jobs SET status=$st WHERE job_id=$id";
    $resource = mysql_query($query) 
        or die (mysql_error());

}
?>
Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Aug 9, 2013 at 11:36 snookiansnookian 8736 gold badges13 silver badges35 bronze badges 4
  • I use X-editable vitalets.github.io/x-editable – JimL Commented Aug 9, 2013 at 11:37
  • Just make an AJAX call to PHP. It is probably most frequently used technology in web – Your Common Sense Commented Aug 9, 2013 at 11:38
  • You could just use PHP's serialize functions to store the form element values in the DB. – cerd Commented Aug 9, 2013 at 11:39
  • 1 refer this link stackoverflow./questions/6108495/… – Ilesh Patel Commented Aug 9, 2013 at 11:43
Add a ment  | 

3 Answers 3

Reset to default 1

Where is the $id in saveStatus.php ?

You need to pass the statusType and job_id via AJAX to update correctly in the saveStatus.php.

For example:

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


<?php foreach ( $results['jobs'] as $job ) { ?>

      <td width="25%"><?php echo $job->job_id; ?></td> 
      <td>
      <select name="status" id="<?php echo $job->job_id; ?>">
              <option value=''>--Select--</option>
              <option value='0'>Approve</option>
              <option value='1'>Reject</option>
              <option value='2'>Pending</option>
      </select>
      <div id="autosavenotify"></div>
      </td>
    </tr>
<?php } ?>

*Note: .live() is deprecated, use .on() instead.

And saveStatus.php

$st = (int)$_POST['statusType'];
$id = (int)$_POST['job_id'];
$query = "UPDATE jobs SET status=$st WHERE job_id=$id";

You will need to use JavaScript (+ jQuery for easier working) to achieve this. Catch the 'change' of the value using JavaScript and fire an AJAX call to a PHP script that saves the value to the MySQL db.

Use jQuery! On the drop-down onchange event do a ajax call and update the record in DB. Here is a link to jQuery Ajax method.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745145980a4613648.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信