Reputation: 873
This is a continuation of a previous post, I am super new to AJAX and as such keep hitting a brick wall. Anways I have the following code:
<script type='text/javascript'>
$(document).ready(function(){
$('#view1, #view2, #view3, #view4, #view5, #view6, #view7, #view8, #view9, #view10, #view11, #view12, #view13, #view14, #view15, #view16, #view17, #view18').on( 'change', 'select', function( ) {
var statusVal = $(this).val();
var job_id = $(this).prop('id');
$.ajax({
type: "POST",
url: "saveStatus.php",
data: { statusType : statusVal, jobID: job_id },
cache : false,
success: function(data) {
$('#view1').load('jobs.php #view1', function() {});
$('#view2').load('jobs.php #view2', function() {});
$('#view3').load('jobs.php #view3', function() {});
$('#view4').load('jobs.php #view4', function() {});
$('#view5').load('jobs.php #view5', function() {});
$('#view6').load('jobs.php #view6', function() {});
$('#view7').load('jobs.php #view7', function() {});
$('#view8').load('jobs.php #view8', function() {});
$('#view9').load('jobs.php #view9', function() {});
$('#view10').load('jobs.php #view10', function() {});
$('#view11').load('jobs.php #view11', function() {});
$('#view12').load('jobs.php #view12', function() {});
$('#view13').load('jobs.php #view13', function() {});
$('#view14').load('jobs.php #view14', function() {});
$('#view15').load('jobs.php #view15', function() {});
$('#view16').load('jobs.php #view16', function() {});
$('#view17').load('jobs.php #view17', function() {});
$('#view18').load('jobs.php #view18', function() {});
}
})
});
});
</script>
Now I have multiple select statements each in a div with the is view1-18, so 18 divs each with a select statement, It works by auto saving the value from the select to my DB, this works great however I am sure this is poorly written code so my first question is help to get it formatted properly.
My second question is what if now i wanted to add two selects in each div and each one getting saved to a different DB field. Below is my select code:
<select name="status" id="<?php echo str_pad($job->job_id, 6, "0", STR_PAD_LEFT); ?>">
<option value=''>--Select--</option>
<option value='Job Approved' <?php if($job->job_schedulestatus == 'Job Approved') echo 'selected'; ?> >Jobs Approved to Print Internally</option>
<option value='To Artwork' <?php if($job->job_schedulestatus == 'To Artwork') echo 'selected'; ?> >To Artwork</option>
</select>
I thought about just adding another and calling it "status2" and creating a duplicate AJAX call and renaming anything where it says status to status2 , this however doesn't work and I have no idea where to go. I have also created a saveStatus2.php which will have the code to insert into the new field.
Any guidance/Help will be greatly appreciated
Ian
Upvotes: 0
Views: 400
Reputation: 978
I would do something like this...
// cache the views
var views = $('.view');
$('#view_wrapper').on('change', 'select', function() {
var statusVal = $(this).val();
var job_id = $(this).prop('id');
$.ajax({
type: "POST",
url: "saveStatus.php",
data: { statusType : statusVal, jobID: job_id },
cache : false,
success: function(data) {
$(views).eatch(function() {
var view_id = $(this).prop('id');
$('#'+view_id).load('jobs.php#'+view_id);
});
}
});
});
Upvotes: 0
Reputation: 380
This seems a little suspect to me - mainly because what I see is the "ajax inception" thing going on. Remember $.load() is also an ajax function, so you are calling multiple .load events as the callback to the main ajax event.
Is there a way to consolidate that into a single Ajax call, as in the first wrapper ajax you have and then let PHP set all the selects for you?
If you have to iterate through each select, I may go this route to make it a little cleaner:
for(var i=1; i<19; i++)
{
$('#view'+i).load('jobs.php#view'+i); //empty callback not needed here as well..
}
Upvotes: 2