javascript variable to php variable

Welfaso

New member
Hello there
I want to assign javascript variable value to a php variable so that I can use that php variable in mysql query. I dont know how to do it.
I am receiving a value from a prompt box(using javascript) and i want that value to be entered into the database.
Kindly help me.

Here is my code:

Code:
<?php


if(isset($_GET['comp_no']))
{
    $complaint_no=$_GET['comp_no'];
    
?>
   <script>
    function myFunction() {
        
    var person = prompt("Enter your name","John Smith");
    
    }
    </script>   
    
<?php
    if(isset($_POST['prog']))
    {
    
    $prog_name=$_POST['prog'];
    
    }
    $query2="update sys_complaints set checked='yes', programmer='$prog_name' where sno='$complaint_no'";
    
    $run_query2=mysql_query($query2);
    if($run_query2)
    {
        echo "<script>alert('Complaint is attended!');</script>";
        header("location:view_complaints.php");
    }
    else
    {
        echo "Query failed!".mysql_error();
    }   
}


?>
 
you'll need to modify your code to use JavaScript to send the entered value to the server-side PHP script. Here's how you can do it:

<?php
if(isset($_GET['comp_no'])) {
$complaint_no=$_GET['comp_no'];
?>

<script>
function myFunction() {
var person = prompt("Enter your name","John Smith");
if (person != null && person != "") {
// Send the entered value to the server-side PHP script using AJAX
var xhr = new XMLHttpRequest();
xhr.open("POST", "process.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
// Handle the response here if needed
alert(xhr.responseText);
} else {
// Handle errors
alert('Request failed!');
}
}
};
xhr.send("comp_no=<?php echo $complaint_no; ?>&prog_name=" + encodeURIComponent(person));
}
}
</script>

<?php
if(isset($_POST['prog_name'])) {
// Get the value sent from JavaScript
$prog_name = $_POST['prog_name'];
// Sanitize and use the value in your SQL query to prevent SQL injection
$prog_name = mysqli_real_escape_string($connection, $prog_name); // Assuming you're using mysqli

$query2 = "UPDATE sys_complaints SET checked='yes', programmer='$prog_name' WHERE sno='$complaint_no'";
$run_query2 = mysqli_query($connection, $query2); // Assuming you're using mysqli
if($run_query2) {
echo "Complaint is attended!";
} else {
echo "Query failed!";
}
}
}
?>
i hope it will work for you. kindly change the file name according to your file
 
JavaScript runs on the client side and PHP runs on the server side, you cannot transfer JavaScript variables directly to PHP variables. Here's how to accomplish it:

JavaScript

<script>
function myFunction() {
var person = prompt("Enter your name", "John Smith");

if (person != null) {

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "update_prog_name.php?person=" + person, true);
xhttp.send();
}
}
</script>


update_prog_name.php
<?php

// Retrieve the value sent by AJAX
if(isset($_GET['person']))
{
$prog_name = $_GET['person'];

// Now you can use $prog_name in your MySQL query. Make sure to sanitize the input to prevent SQL injection
$complaint_no = $_GET['comp_no'];
$prog_name = mysqli_real_escape_string($connection, $prog_name); // Assuming $connection is your database connection

$query2 = "UPDATE sys_complaints SET checked='yes', programmer='$prog_name' WHERE sno='$complaint_no'";
$run_query2 = mysql_query($query2);
if($run_query2) {
echo "Complaint is attended!";
} else {
echo "Query failed!" . mysql_error();
}
} else {
echo "Error: No value received.";
}

?>

I'm hoping it works out for you.
 
Back
Top