Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中调用php代码_Javascript_Php - Fatal编程技术网

如何在javascript中调用php代码

如何在javascript中调用php代码,javascript,php,Javascript,Php,我想检查表单的输入是否为空,然后我想使用php代码进行用户注册 <script> $('#submitbutton').click(function(){ if($('#usernameinput').val() == ''){ alert('Input is blank'); } else { //here i want to use php codes for example

我想检查表单的输入是否为空,然后我想使用php代码进行用户注册

    <script> 
    $('#submitbutton').click(function(){
       if($('#usernameinput').val() == ''){
          alert('Input is blank');
       }
       else {
             //here i want to use php codes for example;
$bgl = new mysqli("localhost", "root", "", "users");
if ($bgl->connect_errno) {
echo "Failed to connect to MySQL: (" . $bgl->connect_errno . ") " . $bgl->connect_error;
}
$username = mysql_real_escape_string($_POST['username']);
          .....
          .....
          ..... /and so on.. 
       }
    });
    </script>

$(“#提交按钮”)。单击(函数(){
if($('#usernameinput').val()=''){
警报(“输入为空”);
}
否则{
//这里我想用php代码为例;
$bgl=新的mysqli(“本地主机”、“根用户”、“用户”);
如果($bgl->connect\u errno){
echo“未能连接到MySQL:(“$bgl->connect\u errno.”“$bgl->connect\u error;
}
$username=mysql\u real\u escape\u字符串($\u POST['username']);
.....
.....
……等等。。
}
});

在本例中,您很可能希望使用
$.ajax()
调用提供此信息的PHP页面


没有像php和javascript那样协同工作的东西。Javascript在客户端运行,php在服务器端运行。最好是在php页面之间传递和检索值,然后根据需要使用这些值。

这里有一些选项。第一种方法是在JavaScript验证之后将表单发布到PHP页面(只需使用表单操作),或者使用Ajax并调用PHP页面,正如Robin所说。

这是不可能的,您可以将JavaScript嵌入PHP代码,但反之亦然。
实现这一点的最佳方法是开发REST服务并使用AJAX检索数据。

在键入问题标题时,会显示一个列表。单击这些项搜索POST方法