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']) . "'/>
1
  • 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) Commented Jun 4, 2016 at 6:48

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

10 Comments

NP :). Questions like this make me feel nostalgic for my first programming days :')
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!:)
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); }}
cant.. they will give me an error when i change all to that
@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);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.