Pausing PHP code execution with alert box

Associate
Joined
6 Mar 2009
Posts
495
Im looking to be able alert the user with some sort of alert box that they have to click confirm in order for the rest of the code to be executed.

Basically what is happening is the user inputs data into a table and depending on the data inputted the result will either pass or fail. It can pass under 3 different specification ranges or just fail, or there can be 4 different outputs.

So far when the submit button is clicked it displays the results to the user and immediately stores it into the database. I want some sort of pop up box which the user has to click confirm before the results are stored in the database.

Can someone point me in the right direction on how to do this!

Thanks
 
Associate
OP
Joined
6 Mar 2009
Posts
495
Use JS/jQuery to do checking on the data, alert the user of error if need be, then use AJax to submit the data to the PHP script where you check the data again (to prevent users circumventing the JS checks) and either return an error (output text/JSON which you can pick up in the AJax events) or insert the data to DB.

Edit - Have a look at handling forms under jQuery (http://api.jquery.com/submit/) and AJax (http://api.jquery.com/category/ajax/). If you use jQuery then it's pretty straight forward to implement.
And if you get stuck then ask; someone on here will happily help

Thanks visibleman, will look into this and will keep yous posted cause may need help:)

Cheers
 
Associate
OP
Joined
6 Mar 2009
Posts
495
Ok guys back again for help!:)

So im using JS and Ajax to run a PHP script without reloading the page.
It is working in the sense that it runs the code in the php file and displays a message of whether or not the test has passed or failed. What it is doing is going right through to the end else statement and not picking up the right response that it should have been.

Think the issue is that the values in the table are not being properly passed into the php script and therefore defaulting to the else statement.

Here is my code:
Code:
<?php
include '../dbConnect.php';
session_start();		
	
	// reading spec values into variables to be used in the if statements below 
		
		$result = mysql_query("SELECT * FROM 4mmchipsspec");
			if (!$result) {
   			 echo 'Could not run query: ' . mysql_error();
   			 exit;
			}
			$row = mysql_fetch_row($result);

			$row[0]; // Example S1TS1
			$row[1]; 
			$row[2];
			$row[3];
			$row[4];
			$row[5];
			
			//PASS TARGET SPEC
				if((("p8_00mmP" ==$row[0])) && (("m4_00mmP">$row[2]) && ("m4_00mmP"<=$row[3])))
				
				
					echo ("Limestone 4-8mm Chips has PASSED with Target Specification!!");	
					
				//PASS NON CONFORMANCE
			if((("p8_00mmP" ==$row[1])) && (("m4_00mmP">=$row[4]) && ("m4_00mmP"<=$row[5])))
				
					echo ("Limestone 4-8mm Chips has PASSED with Non Conformance Specification!!");	
					
					else
						
							//FAIL
							echo ("Limestone 4-8mm Chips has FAILED!! Please Re-Test This Product!!");	

?>

So the "m4_00mmP" for example is a field in the a table thats value should be passed into this page.

Could someone help please!

Cheers
 
Associate
OP
Joined
6 Mar 2009
Posts
495
Ok should have made it a little clearer.

The "4mmChipsSpec" holds the specification values which a product is tested against. I have it working so that it posts the data and submits to the database and tells the user the outcome of the test. I want to be able to "Check" the test to see what the outcome will be before actually posting the data into the db. That's why Im using ajax so it can tell me without reloading the page and loosing the data in the table fields!

So ideally there will be a "submit" button that adds the data to the db and then a separate "Check" button that can tell the user whether the test has passed or failed with the current values entered before submitting to the database.
 
Last edited:
Associate
OP
Joined
6 Mar 2009
Posts
495
Basically all im looking for is AJAX to run and php script to check a test result.

But the issue is that the values in the table are not being passed into the php script cause I am getting a "Undefined index" message for the table values.

It works fine when I submit and POST the values like a normal form.
So im asking how do I pass values from one page to another by using ajax to run the php script.

This is the code for the clickable text that loads the php file.
Code:
<div id="ajaxlink" onclick="loadurl('4mmCheck.php')">Check Test</div>

This is the php code.
Code:
$result = mysql_query("SELECT * FROM 4mmchipsspec");
			if (!$result) {
   			 echo 'Could not run query: ' . mysql_error();
   			 exit;
			}
			$row = mysql_fetch_row($result);

			$row[0]; // Example S1TS1
			$row[1]; 
			$row[2];
			$row[3];
			$row[4];
			$row[5];
			
			//PASS TARGET SPEC
				if((($_POST['p8_00mmP'] ==$row[0])) && (($_POST['m4_00mmP']>$row[2]) && ($_POST['m4_00mmP'] <=$row[3])))
				{
				
					echo ("Limestone 4-8mm Chips has PASSED with Target Specification!!");	
				}
					
				//PASS NON CONFORMANCE
			if((($_POST['p8_00mmP'] ==$row[1])) && (($_POST['m4_00mmP']>=$row[4]) && ($_POST['m4_00mmP'] <=$row[5])))
			{
				
					echo ("Limestone 4-8mm Chips has PASSED with Non Conformance Specification!!");	
			}
					else
						
							//FAIL
							echo ("Limestone 4-8mm Chips has FAILED!! Please Re-Test This Product!!");
When it runs it says "Undefined index" for the "p8_00mmP" values and the other values in the table. It also runs through the statements and prints the else statement so it always shows the FAILED statement at the bottom. So I take it that it cant see the values as there aren't passed from the previous page. So what do I do??

Sorry that its not the best way to do it but if it works it will suits the needs of this project.

Thanks
 
Associate
OP
Joined
6 Mar 2009
Posts
495
Edit: just seen you've said 'var_dump($POST)' - is that a typo? It needs to be $_POST
Yea sorry that was I typo in that post.

The loadurl function:
Code:
function loadurl(dest) {

try {

xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {

}


xmlhttp.onreadystatechange = triggered;


xmlhttp.open("GET", dest);


xmlhttp.send("null");
}

function triggered() {
if ((xmlhttp.readyState == 4)&& (xmlhttp.status == 200)) {

document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
}
}
PS got this code online!
 
Associate
OP
Joined
6 Mar 2009
Posts
495
There is absolutely no requirement for "ajax" at all here. Submit the form, insert the data. Done. If the data fails to insert, return and display a message.

Get it working without javascript before adding complexity by trying to do it with it.
Yous are missing the point here. I am using ajax as it will tell the test result without having to reload the page. I want to use this feature so that I dont have to submit the form to tell me the outcome. I have a submit function working but this is an extra feature that I need!
 
Associate
OP
Joined
6 Mar 2009
Posts
495
I understand, your wanting to validate the outcome as the user enters the result and then you'll save it on submit.

But what we'retrying to explain is that you really don't need to, you're over complicating the process and increasing the number of requests required for something that can be done in a single POST.

Yea I probably am over complicating things! lol I initially wanted to add a line into my single POST where I could almost pause the code execution and let the use click ok or cancel.

So the user clicks submit, it returns what the outcome is and then prompts the user to continue or cancel. If ok or continue is clicked then the rest of the code would be executed and the data submitted to the database.

If I could add something like the following into the php code to prompt the user would be perfect:
Something like that jester mentioned:

Code:
confirm('You sure, mate?')
 
Associate
OP
Joined
6 Mar 2009
Posts
495
You can and people have been hinting at how you could do this, I mean all you really need is to add an additional step between confirming the results and storing them;

1. User submits form.
2. Return confirmation of results.
3. User confirms submission.
4. Save to DB.

Simple.

Ok I have got the confirmation box to appear when I want it, but if I click on cancel it still submits the data to the database!

Here is what I used:
Code:
echo '<script type="text/javascript">
confirm("Are you sure!");
</script>';
 
Back
Top Bottom