Skip to content Skip to sidebar Skip to footer

Assign "div Id" Value To The Variable Php

I have a div on my page '.php'
This prints an id for each product on the page. Ex: 126500 How do I assign this value of the other PHP variabl

Solution 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:

<ahref='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($resultAS$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($plansAS$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($plansAS$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.

Solution 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);
    returntrue;
  })

Post a Comment for "Assign "div Id" Value To The Variable Php"