Home >Backend Development >PHP Tutorial >javascript - How to relate values to each other in js and php?
js part:
$('.new-title-info-btn').mouseenter(
<code> 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); }) } ) </code>
php:
$type=@$_GET['type'];
$sleword=@$_GET['data'];//Get the data sent by js here
switch ($type) {
<code>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;</code>
}
The questions are all written in the comments. They are newbie questions, so if there are any wrong ideas, thank you for pointing them out. If possible, I hope you can help me see how to write the method I am thinking of now, thank you
js part:
$('.new-title-info-btn').mouseenter(
<code> 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); }) } ) </code>
php:
$type=@$_GET['type'];
$sleword=@$_GET['data'];//Get the data sent by js here
switch ($type) {
<code>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;</code>
}
The questions are all written in the comments. It’s a newbie question, so if there are any wrong ideas, thank you for pointing them out. If possible, I hope you can help me see how to write the method I am thinking of now, thank you
$.ajax
Add a field to the parameter dataType: 'json'
, and then use the json_encode
function to make the data you want to pass to js in php into a json string echo
or die
and just go out .