-1

hope that this time I won't mess up :) for people not to missunderstand me that I want to Set Session variable using javascript in PHP, please read this line:

echo "<td><input type='button' id=$type value='Buy!' onclick=location.href='buyone.php'></td>";

I want to have the button ID increase from 1 to 5, but seems like I only get all button have id=5 after the 5-times loops.

while($row=mysqli_fetch_array($result)){
    $type = $row['petID'];
    echo "<tr>";
    echo "<td>" .$type. "</td>";
    echo "<td>" .$row['price']. "</td>";
    echo "<td><input type='button' id=$type value='Buy!' onclick=location.href='buyone.php'></td>";
    echo "<tr>";
}

The $type value is from 1 to 5, because I have 5 rows with ID from 1 to 5 in database. I want to create a table where each row have a button, that's if I click the button, I'll insert the a newline in database. That's a different story, but this's what I want now: when I click a button, I can save the id of that row in $_SESSION['type'] to use in others PHP file. So I use this:

<script>
    $("#1").click(function(){
        <?php $_SESSION['type'] = 1;?>
    });
    $("#2").click(function(){
        <?php $_SESSION['type'] = 2;?>
    });
    $("#3").click(function(){
        <?php $_SESSION['type'] = 3;?>
    });
    $("#4").click(function(){
        <?php $_SESSION['type'] = 4;?>
    });
    $("#5").click(function(){
        <?php $_SESSION['type'] = 5;?>
    });
</script>

But whatever I clicked, the $_SESSION['type']'s value is still 5. I think it's because the button's id is always $type last value after the loop, so all the id is 5, that's why I can only get 5 when I run it.

What should I do to fix it? Also, I want to have a loop in the script too, but I'm not sure how to use it.

So thanks in advance, and I'm here waiting for all your comments :)

Fuuka Adachi
  • 23
  • 1
  • 7
  • 5
    You are mixing the PHP with javascript, PHP is server side evaluated and jQuery/js is client side, that won't work, you can use cookies in js though and then capture the variables in your PHP script. – e-nouri Oct 17 '14 at 07:35
  • Thanks, I'll go to google and search for how to use cookies now :) – Fuuka Adachi Oct 17 '14 at 07:38
  • @karoly: that's not the real problem, the real thing that's bugging me is this line: echo ""; I want to set the button id from 1 to 5, but seems like it's all 5 after the loop. – Fuuka Adachi Oct 17 '14 at 07:40
  • 1
    you change id=$type to id=$row['id'], but seriously you are mixing stuff, you should worry about understanding the basics first, then you can figure out how to do what you want. – e-nouri Oct 17 '14 at 07:56
  • First you need to fix the HTML in this code: ` – Wissam El-Kik Oct 17 '14 at 08:04
  • @e-nouri: So what should I do in this situation, while I want to add a database data inside the html code? thanks for your advice, I know I'm bad so I'll try to fix it :' – Fuuka Adachi Oct 17 '14 at 08:12
  • @wissam: omfg, I'm soo stupid, I'll try it right now :D – Fuuka Adachi Oct 17 '14 at 08:14
  • @FuukaAdachi you are not bad, just a bit mixed, just understand the basics and then you can figure out how to do what you want and ask smart questions ;) ! – e-nouri Oct 17 '14 at 08:29

2 Answers2

0

First understand that PHP is server side evaluated and jQuery/js is client side, so don't get mixed. Second in jQuery/javascript you can't use PHP session directly, you have to use cookies through jQuery/javascript, check this jQuery pug-in, then get the cookies in you PHP script.

Also for <input type='button' id=$type> change $type to $row['id'] to get the id/primary index of your table.

Also about this code, you can factories it, make it more universal, think how you can name your inputs smartly then capture that dynamically, instead of hard coded ones like this, because if you have more than 5 pets what are you going to do, add more :D ?!

<script>
    $("#1").click(function(){
        $.cookie('type', '1');
    });
    $("#2").click(function(){
        $.cookie('type', '2');
    });
    $("#3").click(function(){
        $.cookie('type', '3');
    });
    $("#4").click(function(){
        $.cookie('type', '4');
    });
    $("#5").click(function(){
        $.cookie('type', '5');
    });
</script>
e-nouri
  • 2,576
  • 1
  • 21
  • 36
0

change it as following

<script>
$(document).ready(function(){
    $("#1").click(function(){

$.post( 
             "yourfilename.php",
             { myid: "1" },
             function(data) {
                alert('session set');
             }

          );



    });

})

</script>

in php

if(isset($_POST['myid']))
{
  $_SESSION['type'] = $_POST['myid'];
}

use it for others also