简体   繁体   English

将“ div id”值分配给变量php

[英]Assign “div id” value to the variable php

I have a div on my page '.php' 我的页面“ .php”上有一个div

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

This prints an id for each product on the page. 这会在页面上为每个产品打印一个ID。 Ex: 126500 例如:126500

How do I assign this value of the other PHP variable (not click, on load for example) 如何分配其他PHP变量的此值(例如,未单击,例如在加载时)

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. 如果您正在寻找负载的东西,那么您将需要通过某种Ajax调用使用Javascript。

Your javascript would need to grab the id and then pass that to .php as a POST or GET variable. 您的JavaScript需要获取ID,然后将其作为POST或GET变量传递给.php。

However, it would probably be better for you to pass the ID with the page request. 但是,最好将ID与页面请求一起传递。 So your link to .php would look like this: 因此,您指向.php的链接如下所示:

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

Obviously you can auto generate these. 显然,您可以自动生成这些。 Then in .php you just get the value from GET: 然后在.php中,您只需从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). 然后,可以在SQL查询中使用它(您应该查看PDO而不是mysql_query来保护自己免受SQL注入黑客的攻击)。


Some more detail on the GET option. 有关GET选项的更多详细信息。
Your initial HTML for the menu choice of products to look at would look like this: 您要查看的菜单产品的初始HTML如下所示:

<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: 然后,您的product.php文件将具有以下内容:

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']; 您应该对$ _GET ['id']做进一步的验证; for example check that it is a number and within a range that you would expect for your product_id's 例如,检查该数字是否在预期的product_id的范围内


More detail on the POST option using Javascript & JQuery. 有关使用Javascript和JQuery的POST选项的更多详细信息。 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: 如果您出于特定原因想要进行POST,或者出于某些原因而只想将ID代码发布在一页上,则可以这样进行:

In your HTML I would use a data attribute in a div: 在您的HTML中,我将在div中使用data属性:

<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 (ie do this after the page has loaded): 在Javascript中(我假设使用JQuery,但其他库使用的方法相同),您将添加到“ onload”函数中(即在页面加载后执行此操作):

$('#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 : 最后,您需要一个页面来响应Ajax调用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. 我应该指出,我尚未测试此代码,因此如果直接复制它,请注意拼写错误。

If you're using form, you can put the div's value inside an input[hidden] inside the form. 如果您使用的是表格,则可以将div的值放在表格内的input[hidden] Something like this: 像这样:

  $('#your_form').on('submit', function(){
    your_div = $('#your_div').attr('id');
    $('#hidden_field_used_on_form').val(your_div);
    return true;
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM