Jump to content

grab php variable from another file


jcjst21

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

}

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.