Assign “div id” value to the variable php

烈酒焚心 提交于 2019-12-11 09:38:10

问题


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


回答1:


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.




回答2:


If you're using form, you can put the div's value inside an input[hidden] inside the form. Something like this:

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


来源:https://stackoverflow.com/questions/27943206/assign-div-id-value-to-the-variable-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!