分配“div id”值给变量php [英] Assign "div id" value to the variable php

查看:161
本文介绍了分配“div id”值给变量php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一个div'.php'

 < div id =force_id>< / DIV> 

这会在页面上打印每个产品的ID。例如:126500

如何分配另一个PHP变量的这个值(例如,在加载时不点击)

我在这里使用它:

  $ limit = ?????? (值应该是这里的id值,例如126500)
$ plans = mysql_fetch_array(mysql_query(select * from motors WHERE prod_id ='$ limit'));

预先感谢您...

解决方案

如果您正在寻找负载情况,那么您需要使用JavaScript进行某种Ajax调用。

javascript需要获取这个id,然后将它作为POST或GET变量传递给.php。



但是,将ID传递给页面请求。所以你的.php链接如下所示:

 < a href ='page.php?id = 126500'> ; LINK< / A> 

显然你可以自动生成这些。然后在.php中,您只需从GET获得值:

  $ limit = $ _GET ['id'] 

然后你可以在你的SQL查询中使用它(你应该看看 PDO 而不是mysql_query来保护自己免受SQL注入攻击。)






有关GET选项的更多详细信息。

您选择的产品菜单选项的初始HTML将如下所示:

 < h1>选择要查看的产品< / h1> 
< ul>
<?php
$ result = / *选择我的产品代码* /

foreach($ result AS $ row){
echo< li> < a href ='product.php?id =。$ row ['id']。'> Product。$ row ['name']。< / a>< / li>;
}
?>
< / ul>

然后你的product.php文件就会有这样的结果:

  if(isset($ _ GET ['id'])){
$ limit = $ _ GET ['id'];
$ plan = / * SELECT FROM MOTORS USING $ limit * /
foreach($ plans AS $ row){
echo'Product name:'。$ row ['name'];
echo< img src ='。$ row ['img_path']。'/>;
...
}

} else {
echo'抱歉,无法识别的产品';
}

您应该对$ _GET ['id'];例如检查它是一个数字,并且在您期望的产品ID范围内






有关POST的更多详细信息选项使用Javascript& JQuery的。
如果您有特定的原因想要进行POST,或者出于某种原因,您只想在一个页面上发布ID代码,那么您可以这样做:



在您的HTML中,我会在div中使用数据属性:

 < div id ='product'数据-ID = '12650' >< / DIV> 

在Javascript中(我假设JQuery的方法与其他库相同)添加到'onload'函数中(即在页面加载完成后执行此操作):

  $('#product')。每个(函数(){
var id = $(this).data('id');
$ .ajax({
type:'POST',
url: 'product_content.php',
data:{'id':id},
success:function(msg){
$(this).html(msg);
} ,
错误:函数(msg,status,err){
$(this).html(Product not found);
}
});
});

最后,您需要一个页面来响应Ajax调用product_content.php:

  $ limit = $ _ POST ['id']; 
/ *在$限制做一些验证在这里或者使用PDO作为保护* /
$ plans = / *从马达选择使用$ limit * /
foreach($ plans AS $ row){
echo'Product name:'。$ row ['name'];
echo< img src ='。$ row ['img_path']。'/>;
...
}

我应该指出我没有测试过这个代码,所以如果你直接复制它,请留意错字。


I have a div on my page '.php'

<div id="force_id"></div>

This prints an id for each product on the page. Ex: 126500

How do I assign this value of the other PHP variable (not click, on load for example)

I use it here :

$limit = ??????   (Value should be the id value here. For example 126500)
$plans = mysql_fetch_array(mysql_query("select * from motors WHERE prod_id = '$limit'"));

Thank you in advance ...

解决方案

If you are looking for something on-load then you will need to use Javascript with some sort of Ajax call.

Your javascript would need to grab the id and then pass that to .php as a POST or GET variable.

However, it would probably be better for you to pass the ID with the page request. So your link to .php would look like this:

<a href='page.php?id=126500'>Link</a>

Obviously you can auto generate these. Then in .php you just get the value from GET:

$limit = $_GET['id']

Then you can use it in your SQL query (you should look at PDO rather than mysql_query to protect yourself from SQL injection hacks).


Some more detail on the GET option.
Your initial HTML for the menu choice of products to look at would look like this:

<h1>Select the Product you want to see</h1>
<ul>
<?php
$result = /*  SELECT MY PRODUCT CODES */

foreach($result AS $row){
    echo "<li><a href='product.php?id=".$row['id']."'>Product  ".$row['name']."</a></li>";
}
?>
</ul>

Then your product.php file would have this:

if(isset($_GET['id'])){
    $limit=$_GET['id'];
    $plans= /* SELECT FROM MOTORS USING $limit */
    foreach($plans AS $row){
            echo 'Product name:'.$row['name'];
            echo "<img src='".$row['img_path']."' />";
            ...
     }

}else{
    echo 'Sorry, unrecognised product';
}

You should do further validation on $_GET['id']; for example check that it is a number and within a range that you would expect for your product_id's


More detail on the POST option using Javascript & JQuery. If you have a specific reason for wanting to do POST, or for some reason you only want to publish the ID code on the one page then you could do it like this:

In your HTML I would use a data attribute in a div:

<div id='product' data-id='12650'></div>

In Javascript (I've assumed JQuery but the approach would be the same with other libraries) you would add to your 'onload' function (i.e. do this after the page has loaded):

$('#product').each(function(){
     var id=$(this).data('id');
     $.ajax({
        type: 'POST',
        url: 'product_content.php',
        data: {'id':id},
        success: function(msg){
             $(this).html(msg);
         },
        error: function(msg, status,err){
             $(this).html("Product not Found");             
         }
     });            
});

Finally you need a page to respond to the Ajax call product_content.php :

$limit=$_POST['id'];
/* DO SOME VALIDATION ON $limit HERE OR USE PDO AS PROTECTION */
$plans= /* SELECT FROM MOTORS USING $limit*/
 foreach($plans AS $row){
    echo 'Product name:'.$row['name'];
    echo "<img src='".$row['img_path']."' />";
    ...
}

I should point out I've not tested this code, so keep an eye out for typo's if you copy it directly.

这篇关于分配“div id”值给变量php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆