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 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟