JS section:
$ ('. New-title-info-btn '). MouseEnter (
function(){ var key=$(this).attr('class').split(" ")[1];//这里key取单独给他们的class值 $('.'+key).click(function(){ $.ajax({ url:"js4-1.php?type=findnew", data:key, //我想把变量key的值当数据发送给php,这里不知道怎么写才能把变量key的值发给php success:function(key){ //这里我想获取到php回调的两个数据(new_title,new_class的值),然后反馈给前段修改主页面内容? console.log(key); }, error:function(e){ console.error(e); } }); //console.log(key); }) })
Php:
$type =@$_get[' type '];
$sleword =@$_get[' data '];//here to get JS sent over
Switch ($type) {
case findnew: $sql="select new_title,new_class from info_look";//这里的语句,我想把变量$sleword当作条件加到where后面即select new_title,new_class from info_look where new_calss=$sleword这样子,具体要怎么写才能在sql语句里应用变量sleword? $keyword=mysql_query($sql);//执行语句 $wordArray=[];//创建空字符串承载获取到的数据 while($row=mysql_fetch_array($keyword)){ //$wordArray=$row; //这里能不能return回调$sql查询到的new_title,new_class给js,然后让js插入到指定的页面标签中去? } //print_r($wordArray); break;
}
The questions are written in the comments, the new problem, so there is a wrong place to say thank you for pointing out, if you can, I hope you help me to see how I want to write this method, thank you
Reply content:
JS section:
$ ('. New-title-info-btn '). MouseEnter (
function(){ var key=$(this).attr('class').split(" ")[1];//这里key取单独给他们的class值 $('.'+key).click(function(){ $.ajax({ url:"js4-1.php?type=findnew", data:key, //我想把变量key的值当数据发送给php,这里不知道怎么写才能把变量key的值发给php success:function(key){ //这里我想获取到php回调的两个数据(new_title,new_class的值),然后反馈给前段修改主页面内容? console.log(key); }, error:function(e){ console.error(e); } }); //console.log(key); }) })
Php:
$type =@$_get[' type '];
$sleword =@$_get[' data '];//here to get JS sent over
Switch ($type) {
case findnew: $sql="select new_title,new_class from info_look";//这里的语句,我想把变量$sleword当作条件加到where后面即select new_title,new_class from info_look where new_calss=$sleword这样子,具体要怎么写才能在sql语句里应用变量sleword? $keyword=mysql_query($sql);//执行语句 $wordArray=[];//创建空字符串承载获取到的数据 while($row=mysql_fetch_array($keyword)){ //$wordArray=$row; //这里能不能return回调$sql查询到的new_title,new_class给js,然后让js插入到指定的页面标签中去? } //print_r($wordArray); break;
}
The questions are written in the comments, the new problem, so there is a wrong place to say thank you for pointing out, if you can, I hope you help me to see how I want to write this method, thank you
$.ajaxparameter to add a field dataType: 'json' , and then in PHP to the data you want to pass to JS to use a function to make a json_encode JSON string echo or die go out on the line.