php PHP and Javascript Question
Go to solution
Solved by Mr. E,
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!
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 accountSign in
Already have an account? Sign in here.
Sign In Now