Jump to content

PHP and Javascript Question

Go to solution Solved by Mr. E,

@Kris M 

There are 2 options you can do to run a javascript inside your php script.

 

1st option: by executing "echo()" php function

<?php
if(isset($_POST['submit'])){
	$uname = mysql_prep($_POST['uname']);
	$pass = mysql_prep($_POST['pass']);
	$name = mysql_prep($_POST['name']);
	$sql = "INSERT INTO tbl_users(username,password,name) VALUES('$uname','$pass','$name')";
	if(mysql_query($sql)){
		echo '<script type="text/javascript">';
    	echo ' alert(\'aw\')';
    	echo '</script>';
    }else{
		$result = "Something went wrong. Please contact your IT administrator if this issue persists.";
	}
}
?>

2nd option: by closing your php tag and opening a new php tag inside your if clause. 

<?php
if(isset($_POST['submit'])){
	$uname = mysql_prep($_POST['uname']);
	$pass = mysql_prep($_POST['pass']);
	$name = mysql_prep($_POST['name']);
	$sql = "INSERT INTO tbl_users(username,password,name) VALUES('$uname','$pass','$name')";
	if(mysql_query($sql)){
		//add a php closing and adding new php opening tag inside your if clause.
		?>
		<script type="text/javascript">
			alert('Your message here');
			//if you want to redirect the user
			window.location= "page.php";
          	//you may add other javascript functions here.
		</script>
		<?php
	}else{
		$result = "Something went wrong. Please contact your IT administrator if this issue persists.";
	}
}
?>

I'd prefer using 2nd way. it more efficient than echoing every line of your javascript codes. And another advantage of this is if your code editor does have IntelliSense/Code assistance/Code suggestion. It will continue to provide you code suggestions. Thanks!

Here's my code in PHP :

if(isset($_POST['submit'])){
$uname = mysql_prep($_POST['uname']);
$pass = mysql_prep($_POST['pass']);
$name = mysql_prep($_POST['name']);
$sql = "INSERT INTO tbl_users(username,password,name) VALUES('$uname','$pass','$name')";
	if(mysql_query($sql)){
		//codes here to display popup message for successfully added account.
	}else{
		$result = "Something went wrong. Please contact your IT administrator if this issue persists.";
	}
}

$result variable is displayed after submitting the form. So if the query was not successful, it will display the error message on the form.

Is it possible to do a javascript alert when the php code is successful? Can anyone help me. Thanks.

Link to comment
https://linustechtips.com/topic/551399-php-and-javascript-question/
Share on other sites

Link to post
Share on other sites

Assuming that this is from an ajax reuest and you have something like this

				xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						//success
					}
				}
				xhr.open("GET", "list_back.php?d=" + id, true);
				xhr.send();

Then you would use your php to echo something that you can search for in JS. So if you echo 'success' you can run a JS function. So something like 

if(variable.indexOf('success') == -1){
	alert('PHP not sucessful')
}else{
	alert('Was successful');
}

 

 

 

 

Alternatively, if this is just a PHP script that executes on page load it's a little easier. Use PHP to echo something like

<script>var successfulQuery = true;</script>

Then you can have a simple if statement in javascript checking whether it is true or not

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

4 minutes ago, Hazy125 said:

Assuming that this is from an ajax reuest and you have something like this


				xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						//success
					}
				}
				xhr.open("GET", "list_back.php?d=" + id, true);
				xhr.send();

Then you would use your php to echo something that you can search for in JS. So if you echo 'success' you can run a JS function. So something like 


if(variable.indexOf('success') == -1){
	alert('PHP not sucessful')
}else{
	alert('Was successful');
}

 

 

 

Alternatively, if this is just a PHP script that executes on page load it's a little easier. Use PHP to echo something like


<script>var successfulQuery = true;</script>

Then you can have a simple if statement in javascript checking whether it is true or not

@Hazy125. Thanks for the feedback and solution. But I am not calling an ajax request. i'm running the query on php.

Link to post
Share on other sites

2 minutes ago, Mr. E said:

Yes, It's possible to do an alert between your if clause. You just have to close your php tag and add another opening php tag inside your if clause.

 

@Mr. E Thanks for your feedback. I'll try what you said and get back after trying what you said.

Link to post
Share on other sites

@Kris M 

There are 2 options you can do to run a javascript inside your php script.

 

1st option: by executing "echo()" php function

<?php
if(isset($_POST['submit'])){
	$uname = mysql_prep($_POST['uname']);
	$pass = mysql_prep($_POST['pass']);
	$name = mysql_prep($_POST['name']);
	$sql = "INSERT INTO tbl_users(username,password,name) VALUES('$uname','$pass','$name')";
	if(mysql_query($sql)){
		echo '<script type="text/javascript">';
    	echo ' alert(\'aw\')';
    	echo '</script>';
    }else{
		$result = "Something went wrong. Please contact your IT administrator if this issue persists.";
	}
}
?>

2nd option: by closing your php tag and opening a new php tag inside your if clause. 

<?php
if(isset($_POST['submit'])){
	$uname = mysql_prep($_POST['uname']);
	$pass = mysql_prep($_POST['pass']);
	$name = mysql_prep($_POST['name']);
	$sql = "INSERT INTO tbl_users(username,password,name) VALUES('$uname','$pass','$name')";
	if(mysql_query($sql)){
		//add a php closing and adding new php opening tag inside your if clause.
		?>
		<script type="text/javascript">
			alert('Your message here');
			//if you want to redirect the user
			window.location= "page.php";
          	//you may add other javascript functions here.
		</script>
		<?php
	}else{
		$result = "Something went wrong. Please contact your IT administrator if this issue persists.";
	}
}
?>

I'd prefer using 2nd way. it more efficient than echoing every line of your javascript codes. And another advantage of this is if your code editor does have IntelliSense/Code assistance/Code suggestion. It will continue to provide you code suggestions. Thanks!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×