How to run js code in php
1. In this case, ok will not pop up
<?php
session_start();
if($_POST){
if($_POST['mobile']!=$_SESSION['mobile'] or $_POST['mobile_code']!=$_SESSION['mobile_code'] or empty($_POST['mobile']) or empty($ _POST['mobile_code'])){
echo '<script type="text/javascript">';
echo 'alert("ok");';
echo '</script>';
}else{
$_SESSION['mobile'] = '';
$_SESSION['mobile_code'] = '';
}
?>
2. In this case, you can pop up ok
<?php
echo '<script language="javascript">';
echo 'alert("ok");';
echo '</script>';
?>
Is there any difference between the two? Please tell me, thank you
There is no difference. In the first case, you did not enter the if statement, so there is no pop-up box. You can try removing the outer if ($_POST), and you can pop up the box. I guess you haven't sent a post request at all, so you can't enter the if code block.
Dear, this does not run in php.
php -> Output html+js -> The browser receives and executes js -> Result
For your first example, it is the web page returned when making a POST request. Generally, you use a browser to enter the URL, which is a GET request
There is no difference if you look at the code alone, but it may be different if you look at the actual situation. Only if it is judged that there is no entry, the pop-up box will not pop up. You should use ajax in JQ. It’s not difficult to check it out on Baidu