Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get div id into php variable?

How can I get this id into a PHP variable after submit a form? I need to pass the total value to a PHP variable. Please help me to solve this problem.

<div class="table-responsive">
    <table class="table table-bordered">
        <tr>        
            <th>Total Price</th>
        </tr>
        <tr>                 
            <td>
                <input class="form-control1" type='text' id='txt_totalprice' name='txt_totalprice[]'/> 
            </td>
        </tr>
    </table>     
</div>

Total: <div id="totalvalue">0</div>  

Here is the script

<script type='text/javascript'>
    $('#totalvalue').click(function() {
        $.post("package.php", { 
            id: $(this).attr('id') 
        }, function(response) {
            alert(response);
        });
    });
</script>     
like image 490
think_user Avatar asked Sep 02 '26 01:09

think_user


2 Answers

You want the value between the div tags, not the ID, correct?

Change this:

id: $(this).attr('id')

To this:

id: $(this).text()

If you want to display the value on the page do this:

Create an empty div:

<div id="saved-value"></div>

Place it anywhere you want on the page.

Then change your jQuery:

}, function(response) {
    $('#saved-value').html(response);
});
like image 124
awl19 Avatar answered Sep 03 '26 16:09

awl19


This is how you get the id value on your package.php Page.

   <?php 

     $_POST['id'];

    ?>

or you can just store the id in a new variable.

<?php 

$id = $_POST['id'] 
echo($id);
?>

if you arent sure if there are any values being sent by your post you can use this.

<?php 

//This will help you since Post is an array.
 print_r($_POST) 

?>
like image 26
Jabulani Avatar answered Sep 03 '26 15:09

Jabulani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!