How are Javascript-js and PHP related to each other?

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.