问题
How to submit a select value on click of a jquery function and save the values in database and return to the same page?
I'm having this select box, This select box is not inside any form.
<select name="myRating" class="rating" id="serialStar">
<option value="1">Alright</option>
<option value="2">Ok</option>
<option value="3">Getting Better</option>
<option value="4">Awesome</option>
<option value="5">Loved!</option>
</select>
I'm using below jQuery function to send the select value into a controller action 'set_ratings'
$(document).ready(function(){
$(".ui-rating").click(function(){
// we want to store the values from the select box, then send via ajax below
var fid = $('#serialStar').val();
$.ajax({
type: "POST",
url: "http://localhost/reelstubs/movies/set_ratings",
data: "fid="+ fid
});
return false;
});
});
Now I want to save the value into database. How can I achieve this? Is the way proper?
回答1:
the url from ajax must not be relative path from localhost.
instead of
url: "http://localhost/reelstubs/movies/set_ratings"
use
url: "../reelstubs/movies/set_ratings"
or where is your php file
The way to add value to database, learn from here: http://www.ibm.com/developerworks/opensource/library/os-php-jquery-ajax/ (this is an example)
good luck
来源:https://stackoverflow.com/questions/11670998/store-value-in-database-on-click-of-a-jquery-function-by-fetching-the-values-fro