Javascript-php get to the data parameter, how to match the array

Source: Internet
Author: User
Tags switch case

1.html:

推荐

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

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

//从主页前段到phpcase 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;

}

Features to implement:
After clicking on the recommended button in HTML, Ajax sends two parameters to PHP, one of which is type, which is used to determine if the request is to execute the program of the Switch Case home page in PHP, and the data that is used to judge the New_class field values in the database. The same will send her new_title value to JS, and then JS to replace the value of class= "New-infomation-title" in the "people of all nationalities sent a congratulatory message" content
Problem:
PHP, comments//Here the value of the value of $wordarray[i].new_class to JS here How to write? and the previous for loop PHP seems to be unable to write this, the test will error, ask for answers

Error:

Reply content:

1.html:

推荐

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

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

//从主页前段到phpcase 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;

}

Features to implement:
After clicking on the recommended button in HTML, Ajax sends two parameters to PHP, one of which is type, which is used to determine if the request is to execute the program of the Switch Case home page in PHP, and the data that is used to judge the New_class field values in the database. The same will send her new_title value to JS, and then JS to replace the value of class= "New-infomation-title" in the "people of all nationalities sent a congratulatory message" content
Problem:
PHP, comments//Here the value of the value of $wordarray[i].new_class to JS here How to write? and the previous for loop PHP seems to be unable to write this, the test will error, ask for answers

Error:

The PHP array does not have. length.
To get the array length, use the Count function.

for($i=0;$i
  
   < code="">

处 少写了$ 应该是 $i 而不是 i

楼上是对的。其实你可以把数据放入html的一个p标签里,设置一个id,并且用css隐藏(display:none;)。js直接选取这个p标签,提取出文本,就可以拿到数据了。这是比较粗暴的办法。

  • 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.