Home  >  Article  >  Backend Development  >  javascript - PHP gets the data parameter, how to match it with the array

javascript - PHP gets the data parameter, how to match it with the array

WBOY
WBOYOriginal
2016-08-08 09:06:511194browse

1.html:

<code><div class="new-title-info-btn recommend"><a href="#">推荐</a></div></code>

People of all ethnic groups across the country sent congratulatory messages

2.js:
$('.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", 
                dataType:"json", 
                data:"key="+key//这里必须加个键名"key=",不然key的值就会直接和type的值合在一起,变成type=findnew&pic;加了就是type=findnew&key=pic
            }).done(function(data){
                //
            });
        });
    };
);</code>

3.php:
$type=@$_GET['type'];
$sleword=@$_GET['data'];
switch ($type) {

<code>//从主页前段到php
case findnew:
    $sql="select id,new_title,new_class from info_look";//创建查询语句
    $re=mysql_query($sql);//执行语句
    $wordArray=[];//创建空字符串承载获取到的数据
    while($row=mysql_fetch_array($re)){
        $wordArray[]=$row;//将结果赋值给数组
    }
    //下面打算用循环找到对应传递过来的data参数,匹配到了,就把他回传给前台
     for($i=0;i<$wordArray.length;i++){
             if($wordArray[i].new_class==$sleword){
                 //这里传值$wordArray[i].new_class的值给js
                 //...
                 print_r ($wordArray[i].new_class);
             }
         }
    break;</code>

}
javascript - PHP gets the data parameter, how to match it with the array

Function to be implemented:
After clicking the recommendation button in html, ajax will send two parameters to php. One is type, which is used to determine whether the request is to execute the program in the switch case homepage in php; the other is data , used to determine if it matches the new_class field value in the database. If it is the same, her new_title value is sent to js, ​​and then js replaces the value in class="new-infomation-title". People of all nationalities across the country send congratulatory messages. 'Content
Question:
In php, comment //How to write the value of $wordArray[i].new_class here to js? Also, it seems that the previous for loop cannot be written like this in php. The test will report an error. Please answer.

Error report screenshot:

javascript - PHP gets the data parameter, how to match it with the array

Reply content:

1.html:

<code><div class="new-title-info-btn recommend"><a href="#">推荐</a></div></code>

People of all ethnic groups across the country sent congratulatory messages

2.js:
$('.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", 
                dataType:"json", 
                data:"key="+key//这里必须加个键名"key=",不然key的值就会直接和type的值合在一起,变成type=findnew&pic;加了就是type=findnew&key=pic
            }).done(function(data){
                //
            });
        });
    };
);</code>

3.php:
$type=@$_GET['type'];
$sleword=@$_GET['data'];
switch ($type) {

<code>//从主页前段到php
case findnew:
    $sql="select id,new_title,new_class from info_look";//创建查询语句
    $re=mysql_query($sql);//执行语句
    $wordArray=[];//创建空字符串承载获取到的数据
    while($row=mysql_fetch_array($re)){
        $wordArray[]=$row;//将结果赋值给数组
    }
    //下面打算用循环找到对应传递过来的data参数,匹配到了,就把他回传给前台
     for($i=0;i<$wordArray.length;i++){
             if($wordArray[i].new_class==$sleword){
                 //这里传值$wordArray[i].new_class的值给js
                 //...
                 print_r ($wordArray[i].new_class);
             }
         }
    break;</code>

}
javascript - PHP gets the data parameter, how to match it with the array

Function to be implemented:
After clicking the recommendation button in html, ajax will send two parameters to php. One is type, which is used to determine whether the request is to execute the program in the switch case homepage in php; the other is data , used to determine if it matches the new_class field value in the database. If it is the same, her new_title value is sent to js, ​​and then js replaces the value in class="new-infomation-title". People of all nationalities across the country send congratulatory messages. 'Content
Question:
In php, comment //How to write the value of $wordArray[i].new_class here to js? Also, it seems that the previous for loop cannot be written like this in php. The test will report an error. Please answer.

Error report screenshot:

javascript - PHP gets the data parameter, how to match it with the array

PHP arrays don’t have .length.
To get the array length, please use the count function.

<code>for($i=0;$i<count($wordArray);$i++){
    print_r($wordArray[$i]);
}</code>

The screenshot is missing $, it should be $i instead of i

The person above is right. In fact, you can put the data into a p tag in html, set an id, and hide it with css (display:none;). js directly selects the p tag, extracts the text, and then you can get the data. This is a more crude method.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn