3

I tried using $_SESSION["name"] = "document.write(name)"; and it works if i echo it out. But if i were to show this SESSION variable into a textbox or use it to do SQL, it shows document.write(name). How can i properly store it into a php variable? I've search and many people say you cant convert it. But surely there is someways where i can use the Javascript variable(name) to show it in a textbox or use it in SQL.

javascript code with the variable name.

<script>
    var name;
    $(document).ready(function()
    {
        $('#info tr #name').click(function()
        {
            name = $(this).text();
            alert(name);
            window.location = 'CV.php';
        });
    });
</script>

For SQL my code is this,

<?php $_SESSION["name"] = "<script>document.write(name)</script>"; 
    echo $_SESSION["name"];
    $connect = mysqli_connect("localhost", "root", "","test1");
    $sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";

    $result = mysqli_query($connect, $sql);

    while($row= mysqli_fetch_array($result))
    {
        $name = $row['Name'];
        $number =$row['Number'];
        $Expertise =$row['Expertise'];
        $status =$row['Status'];
        $remarks =$row['Remarks'];
    }
?>

And below is my code to try to show the variable that i have stored in session into textbox.

<input type='text' value='" .htmlspecialchars($_SESSION['name']) . "'/>
Tajmin
  • 353
  • 1
  • 7
  • 23
Samuel
  • 225
  • 3
  • 11
  • 1
    PHP is serverside, document.write is JS, client side. PHP doesn't parse it it echos it as a string. It works when you echo it because your browser does it. But when you put it in text value, browser looks at it as a value (string not function to be rendered) – Moussa Khalil Jun 04 '16 at 06:48

1 Answers1

1
<script>
    var name;
    $(document).ready(function()
    {
    $('#info tr #name').click(function()
    {

        name = $(this).text();
        alert(name);
        window.location = 'CV.php?name=' + name; // send in link

    });
    });
    </script>

Inside CV.PHP

if(isset($_GET['name']))
{
    $name = $_GET['name']; // Get it from URL
    $_SESSION["name"] = $name; 
}
else
{
    $name = $_SESSION['name']; // Get it from session
}
$connect = mysqli_connect("localhost", "root", "","test1");
$sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";

$result = mysqli_query($connect, $sql);

while($row= mysqli_fetch_array($result))
{
   $name = $row['Name'];
   $number =$row['Number'];
   $Expertise =$row['Expertise'];
   $status =$row['Status'];
   $remarks =$row['Remarks'];
} ?>

To show it in input box:

    <input type='text' value='" .$name . "'/> // echo value as string
    <input type='text' value='" .$_SESSION['name']. "'/> // if it's a page that is not CV.php
Moussa Khalil
  • 635
  • 5
  • 12
  • NP :). Questions like this make me feel nostalgic for my first programming days :') – Moussa Khalil Jun 04 '16 at 17:51
  • Hi @MoussaKhalil ! The code you gave to me works! However, after i got to CV.php page, i have an update button which refreshes my page and the $name = $_GET['name'] will not be able to get detected. I think the reason is because the page is not from select.php. It is from CV.php to CV.php. Therefore, do you have any method so that i am still able to capture and store the name somewhere so that even if i refresh the page, the name will not be refreshed. Thank you!:) – Samuel Jun 05 '16 at 13:16
  • omg youre really helping me so so much! Do you think you can help me again? haha because now im having trouble doing the update button. Cant seem to update it. My code for the update is : {if(isset($_POST['update'])) { $UpdateQuery = "UPDATE particulars SET Name = '$_POST[name]', Number= '$_POST[number]', " . "Expertise= '$_POST[expertise]', Status= '$_POST[status]', Remarks= '$_POST[remark]' WHERE Name = '$_POST[hidden]'"; mysqli_query($connect, $UpdateQuery); }} – Samuel Jun 05 '16 at 13:35
  • cant.. they will give me an error when i change all to that – Samuel Jun 05 '16 at 14:16
  • @Samuel Wait that was totally wrong.. try this and tell me the error (if any) $UpdateQuery = "UPDATE particulars SET Name = '". $_POST['name']."', Number= '". $_POST['number']." ', " . "Expertise= '".$_POST['expertise']." ', Status= '".$_POST['status']." ', Remarks= '".$_POST['remark']." ' WHERE Name = '".$_POST['hidden']." '"; mysqli_query($connect, $UpdateQuery); – Moussa Khalil Jun 05 '16 at 14:44
  • Nope it does not show any error. But when i click update it doesnt update. Is there any way where i can show you my code? maybe you can have a better look at it? thanks – Samuel Jun 05 '16 at 14:47
  • @Samuel Echo $_POST['hidden'] and see if it holds a name that exists in the database. If there is no error then it's probably in that – Moussa Khalil Jun 05 '16 at 14:57
  • http://stackoverflow.com/questions/37643744/i-feel-my-date-sql-is-correct-but-idk-why-it-cant-work – Samuel Jun 05 '16 at 15:44
  • Above is my codes. I have just posted them. When i checked the hidden text it shows exactly the name in the database. – Samuel Jun 05 '16 at 15:44
  • http://stackoverflow.com/questions/37228533/submit-button-on-each-row-of-a-table-that-inserts-data-of-that-row-into-database Hi this is my friends work. He want to ask you if you know too haha – Samuel Jun 05 '16 at 15:47