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
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
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 :)
 
Last edited:
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
Joined
10 Nov 2013
Posts
1,808
Could you clarify the structure of your table '4mmchipsspec'? I'm not sure whether 'm4_00mmP' is a column name or a value?

I think you may have some confusion over the structure returned by mysql_fetch_row. This will return a single row from the query results, so $row[0] is accessing the value in the first column, $row[1] gets the value in the second column, etc.

If 'm4_00mmP' is a column name you shouldn't be comparing that with what's in $row[n].
If 'm4_00mmP' is the column value then I wouldn't want to be using greater/less than symbols for comparison.

Just a heads up, if you're wanting to perform your checks for each row returned by your query you will need to set up a loop on the mysql_fetch_row command, e.g.

Code:
while($row = mysql_fetch_row($result))
{
  // do stuff here
}

This way it will stop looping when there are no more rows left.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Why oh why is the first "goto" always "use ajax" in everything anyone ever asks about web dev? What's wrong with a simple post submission and reload? Ajax isn't even required here. Just submit the damn form. Add an onclick event to confirm submission.

Code:
<form action="foo" method="post">
 <!-- yada yada -->
<button type="submit" onclick="return confirm('You sure, mate?')"/>
</form>

Adding that one simple call does everything you require.
 
Last edited:
Soldato
Joined
7 Jun 2003
Posts
6,234
Location
Leicestershire
Unless there's something I'm missing, I'd agree with Jestar. I set up a database form for work using that sort of code.

I have a slightly more complex one where if it fails, the user needs to go back and make corrections. PHP error checking as per usual when the user hits submit. If it fails, it flags and stays on page. If it passes, it gives the user a confirmation box to tick.
 
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
Joined
19 May 2003
Posts
1,390
Location
Saltaire, West Yorkshire
I suggest reading up on some basic form validation with PHP

http://www.sitepoint.com/form-validation-with-php/

No need for AJAX, a simple form post is all you need to do this;

1. User submits form
2. Validate against stored values.
3. Return result to User

If you really expect to get some more help then you'll need to describe your problem better, it's quite awkward to understand what your trying to do.
 
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
Joined
10 Nov 2013
Posts
1,808
OK, so your ajax request is not actually posting any data. Can you paste your loadurl function?
You want to be using $.post() or $.ajax({type: 'post',...})


Edit: just seen you've said 'var_dump($POST)' - is that a typo? It needs to be $_POST
 
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
Joined
10 Nov 2013
Posts
1,808
xmlhttp.open("GET", dest);

...should be...

xmlhttp.open("POST", dest);

...and...

xmlhttp.send(); should contain your form data, not null.

See http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest

For future reference, I'd recommend using jquery if you want to do ajax requests. Although as has already been said, trying to use ajax in this instance has probably made your task unnecessarily complicated.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
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.
 
Associate
Joined
19 May 2003
Posts
1,390
Location
Saltaire, West Yorkshire
Look at the request/response of your AJAX call and make sure it's as expected. No point debugging the JS if your not even POSTing correctly.

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.

Exactly, although doing this via AJAX is hardly complex.
 
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
Joined
19 May 2003
Posts
1,390
Location
Saltaire, West Yorkshire
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!

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.

But keep at it, i'm sure you'll figure it out shortly ;)
 
Back
Top Bottom