dongyi9023 2015-01-14 12:41 采纳率: 0%
浏览 40
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • duan19850312 2015-01-14 13:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大