简体   繁体   中英

Using ajax and updating the database on the same url?

I have a form which has a select tag that contains the numbers from 1-31. What I'm trying to do is update the database with the value in the select tag if the user changes it (the default value is the value in the database ie $drop_d ) WITHOUT reloading the page at all. The following code doesn't seem to work, I think there is a problem in the way I am sending my data to ajax.

This variable contains the default value:

  $drop_d =  $e_r[drop_d];  

The form:

<form> Number of days:  <select name="dropD" id="dropID"> <?php for($i=1; $i<=31; $i++){  ?> <option value="<?php echo $i;?>" <?php if($i==$drop_d){ ?> selected="selected" <?php } ?> > <?php echo $i;  } ?> </select> </form> <br />

Ajax:

<script>
$('#dropID').change(function(){
   alert(1);
    var url = document.URL;
    var blah = $('#dropID').val()
    alert(blah);
    alert(url);
    $.ajax({
        type: 'POST',
        url: url,
        data: {newFieldValue: blah},
        dataType: 'html'
    }).done( function(){
            alert(1);
            blah.hide();
        });
});
</script>

Update on database:

<?php
        $newFieldValue = $_POST['newFieldValue'];
        $updateD = 'UPDATE en SET drop_d = $newFieldValue WHERE name=$name;
        mysql_query($updateD);

             ?>
$('select#dropID').on('change' , function(){
    var url = document.URL;
    var blah =  $('select#dropID').val();
    $.ajax({
        type: 'POST',
        url: url,
        data: {newFieldValue:blah},
        dataType: 'html'
    });
});

您已经定义了id值id="dropID ,因此您的jQuery应该是:

$('#dropID').val()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM