how do i pass JavaScript confirm variable to php variable
One of the best way to pass information from the client side (JS) to the server side (PHP) is to use an html form
. The html form can be hidden
from the webpage if you do not want users to see it.
You want this form to POST
the required information to a PHP script.
For example, you wanted to pass a boolean
variable to the server side which is captured from a confirm box. So, let's make an form using the value returned from the confirm box to be POST to the server side.
The html form:
<form id="myForm" action="testBoolean.php" method="post">
<input type="hidden" id="inputBoolean" value="5" name="boolean">
</form>
The javascript (put this below your form, you may get errors if you don't)
<script>
var jsChoose = confirm("are you sure?");
document.getElementById("inputBoolean").value = jsChoose;
document.getElementById("myForm").submit();
</script>
And now you need a php page called "testBoolean.php" and in it you retrieve the posted information. For this demonstration it will simply echo out the value of the POSTed boolean variable that was given by your confirm box.
testBoolean.php :
<?php
$boolean= htmlspecialchars($_POST['boolean']);
echo $boolean;
?>
Remember what's inside the $_POST['']
brackets is the name
of your input field that you have submitted.
Hope this helps, good luck and best regards.
How do I pass JavaScript variables to PHP?
You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.
<DOCTYPE html>
<html>
<head>
<title>My Test Form</title>
</head>
<body>
<form method="POST">
<p>Please, choose the salary id to proceed result:</p>
<p>
<label for="salarieids">SalarieID:</label>
<?php
$query = "SELECT * FROM salarie";
$result = mysql_query($query);
if ($result) :
?>
<select id="salarieids" name="salarieid">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)
}
?>
</select>
<?php endif ?>
</p>
<p>
<input type="submit" value="Sumbit my choice"/>
</p>
</form>
<?php if isset($_POST['salaried']) : ?>
<?php
$query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];
$result = mysql_query($query);
if ($result) :
?>
<table>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others
echo '</tr>';
}
?>
</table>
<?php endif?>
<?php endif ?>
</body>
</html>
how can i pass js variable to php variable?
<script>
var jsvar = "Name";
</script>
<?php
$phpVar = "<script>document.write(jsvar);</script>";
echo $phpVar;
?>
Pass Javascript Variable Value in PHP
Try below to send the JS data to PHP via AJAX,
$.ajax({
type: 'POST',
url: 'yourphppage.php',
data: {
'totalprice' : $("#total-price").val(),
},
success: function(response){
}
})
In yourphppage.php,
echo $_POST['totalprice'];
Send JavaScript variable to PHP variable
As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name']
after your javascript was executed.
Sending PHP Variable within a JavaScript Alert / Confirm Box
window.location = "http://www.xxxxxxx.co.uk/admin/delete.php?id=<?php echo $clickid; ?>";
Related Topics
How to Redirect to Another Page in Reactjs When a If Condition Is Executed
Css Div Height Not Expanding to Fit Content or Wrapping Content
How to Convert Uint8 Array to Base64 Encoded String
How to Redirect to Another Component Onsubmit Search
Remove Time from Gmt Time Format
Export Array of Objects into Excel Using JavaScript
Handle File Download from Ajax Post
Violation Long Running JavaScript Task Took Xx Ms
Add Parameters to Url Without Refresh
Different Color for Each Bar in a Bar Chart; Chartjs
Convert Date String from Yyyymmdd to Dd Mon Yyyy
Angular2: Show Placeholder Image If Img Src Is Not Valid
How to Simulate Mouse Click Along With Mouse Move Using JavaScript
How to Scroll Left or Right Inside a Div Using Pure JavaScript Function and No Jquery
How to Trigger Click Event on Select Element (Not Change)
Make Discord Bot Send Picture With Message With Nodejs