\";?", "text": "<p>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. </p>\n\n<p>javascript code with the variable name.</p>\n\n<pre class="prettyprint"><code>&lt;script&gt;\n var name;\n $(document).ready(function()\n {\n $('#info tr #name').click(function()\n {\n name = $(this).text();\n alert(name);\n window.location = 'CV.php';\n });\n });\n&lt;/script&gt;\n</code></pre>\n\n<p>For SQL my code is this,</p>\n\n<pre class="prettyprint"><code>&lt;?php $_SESSION["name"] = "&lt;script&gt;document.write(name)&lt;/script&gt;"; \n echo $_SESSION["name"];\n $connect = mysqli_connect("localhost", "root", "","test1");\n $sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";\n\n $result = mysqli_query($connect, $sql);\n\n while($row= mysqli_fetch_array($result))\n {\n $name = $row['Name'];\n $number =$row['Number'];\n $Expertise =$row['Expertise'];\n $status =$row['Status'];\n $remarks =$row['Remarks'];\n }\n?&gt;\n</code></pre>\n\n<p>And below is my code to try to show the variable that i have stored in session into textbox.</p>\n\n<pre class="prettyprint"><code>&lt;input type='text' value='" .htmlspecialchars($_SESSION['name']) . "'/&gt;\n</code></pre>", "answerCount": 1, "upvoteCount": 508, "dateCreated": "2026-07-18 00:20:06", "dateModified": "2026-07-20 14:30:05", "author": { "type": "Person", "name": "Samuel" }, "acceptedAnswer": { "@type": "Answer", "text": "<pre class="prettyprint"><code>&lt;script&gt;\n var name;\n $(document).ready(function()\n {\n $('#info tr #name').click(function()\n {\n\n name = $(this).text();\n alert(name);\n window.location = 'CV.php?name=' + name; // send in link\n\n });\n });\n &lt;/script&gt;\n</code></pre>\n\n<p>Inside CV.PHP</p>\n\n<pre class="prettyprint"><code>if(isset($_GET['name']))\n{\n $name = $_GET['name']; // Get it from URL\n $_SESSION["name"] = $name; \n}\nelse\n{\n $name = $_SESSION['name']; // Get it from session\n}\n$connect = mysqli_connect("localhost", "root", "","test1");\n$sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $_SESSION["name"]."'";\n\n$result = mysqli_query($connect, $sql);\n\nwhile($row= mysqli_fetch_array($result))\n{\n $name = $row['Name'];\n $number =$row['Number'];\n $Expertise =$row['Expertise'];\n $status =$row['Status'];\n $remarks =$row['Remarks'];\n} ?&gt;\n</code></pre>\n\n<p>To show it in input box:</p>\n\n<pre class="prettyprint"><code> &lt;input type='text' value='" .$name . "'/&gt; // echo value as string\n &lt;input type='text' value='" .$_SESSION['name']. "'/&gt; // if it's a page that is not CV.php\n</code></pre>", "upvoteCount": 69, "url": "https://exchangetuts.com/index.php/how-to-store-js-variable-into-php-without-using-sessionname-scriptdocumentwritenamescript-1768501802860825#answer-1784593206957002", "dateCreated": "2026-07-20 00:30:05", "dateModified": "2026-07-20 14:30:05", "author": { "type": "Person", "name": "Moussa Khalil" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store js variable into php without using $_SESSION["name"] = "<script>document.write(name)</script>";?

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']) . "'/>
like image 508
Samuel Avatar asked Jul 18 '26 00:07

Samuel


1 Answers

<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
like image 69
Moussa Khalil Avatar answered Jul 20 '26 14:07

Moussa Khalil