jcjst21 Posted July 31, 2012 Share Posted July 31, 2012 I created a function using jquery to load data from another file to display on my main page. The user will fill out some information on the main page, and I post it to another file and perform the mysql INSERT. I would like to pass the last created id in that table back to the main page to insert it as the default value in another form on the main page... how would I do that with the jquery functions? I thought of the get function, but that didn't work:( Thanks, jcj Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/ Share on other sites More sharing options...
requinix Posted July 31, 2012 Share Posted July 31, 2012 Your script (the one doing the INSERT) has to return the ID number. Easiest way would be with JSON because the JSON encoding of a number is just the number. // do stuff // $id = insert ID header("Content-Type: application/json"); echo $id; Then .get() will tell you what that number is. By the way, if you're submitting information to a script like that then you should be doing a POST, not a GET. Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1365595 Share on other sites More sharing options...
jcjst21 Posted July 31, 2012 Author Share Posted July 31, 2012 Well I did a "POST" function on the main page to send the form data; but I want to GET the id created back... OK so use JSON...so on the main page...how would I call it...in the same response function for the button "on click" function that contains the POST function? Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1365629 Share on other sites More sharing options...
requinix Posted July 31, 2012 Share Posted July 31, 2012 Well I did a "POST" function on the main page to send the form data; but I want to GET the id created back... No. One POST request that returns the new ID number. That's how these things are supposed to work. OK so use JSON...so on the main page...how would I call it...in the same response function for the button "on click" function that contains the POST function? Whatever you're doing now except (a) POST and (b) the callback will get the ID number as the data. Plus the relevant changes to the PHP script of course. Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1365809 Share on other sites More sharing options...
jcjst21 Posted August 1, 2012 Author Share Posted August 1, 2012 ok it's working now, BUT it's return the previously created ID... so when the id should be for instance "3", it's return "2"... i called it this way in the response function function(response) { $("#lastID").val($theMaxID); } Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1365984 Share on other sites More sharing options...
requinix Posted August 1, 2012 Share Posted August 1, 2012 What's the actual, complete code? Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1366104 Share on other sites More sharing options...
jcjst21 Posted August 3, 2012 Author Share Posted August 3, 2012 Thanks for looking at this... Here is the code for my main page: $("#addADose").click(function() { var cMedName=$("#cMedName").val(); var doseAmount=$("#doseAmount").val(); var doseMeasurement=$("#doseMeasurement").val(); var quantity=$("#quantity").val(); var doseMeasurement2=$("#doseMeasurement2").val(); var Sunday = $('#Sunday').attr('checked')?1:0; var Monday = $('#Monday').attr('checked')?1:0; var Tuesday = $('#Tuesday').attr('checked')?1:0; var Wednesday = $('#Wednesday').attr('checked')?1:0; var Thursday = $('#Thursday').attr('checked')?1:0; var Friday = $('#Friday').attr('checked')?1:0; var Saturday = $('#Saturday').attr('checked')?1:0; var doseCount=$("#doseCount").val(); $.post( 'medicationInfoPost.php', { cMedName:cMedName, doseAmount:doseAmount, doseMeasurement:doseMeasurement, quantity:quantity, doseMeasurement2:doseMeasurement2, Sunday:Sunday, Monday:Monday, Tuesday:Tuesday, Wednesday:Wednesday, Thursday:Thursday, Friday:Friday, Saturday:Saturday, doseCount:doseCount }, function(response) { $("#displayMedicationInfo").load("medicationInfoPull.php"); $("#medicationNameID").val(""); $("#doseAmount").val(""); $("#medMeasureNameID").val(""); $("#quantity").val(""); $("#medMeasureNameID").val(""); $("#doseCount").val(""); $("#cMedID").val(""); $("#medID").load("getLastIDCreated.php"); $("#lastID").val($theMaxID); } Then here is the code for the page that does the mySQL insert $sql="INSERT INTO CamperMedicationInfo(userID, etc....) VALUES ('$userID', '$camperID', etc....')"; mysql_query($sql); $cMedNumber=mysql_insert_id(); Then the code that pulls the data $sql2=mysql_query("SELECT * FROM CamperMedicationInfo WHERE camperID=$camperID ORDER BY cMedID"); $maxid=mysql_query("SELECT MAX(cMedID) FROM CamperMedicationInfo WHERE camperID=$camperID"); $maxIDretrieve=mysql_fetch_array($maxid); $theMaxID=$maxIDretrieve[0]; Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1366644 Share on other sites More sharing options...
requinix Posted August 4, 2012 Share Posted August 4, 2012 You're not listening to me. One AJAX request with the new data and the response includes the ID. The same request. Not another request. No more AJAX. It's over. Stop it. Don't .load() or .get() or .post() anything else. Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1366699 Share on other sites More sharing options...
jcjst21 Posted August 4, 2012 Author Share Posted August 4, 2012 Ok then... what do I use for my value to load for a form if it already pulls the id?? the "cMedID" from the response function? Quote Link to comment https://forums.phpfreaks.com/topic/266475-grab-php-variable-from-another-file/#findComment-1366711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.