Send a javascript var to php [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I try to assign a string type javascript variable to a php variable and retrieve a table from database, I wrote all the code in a file.
I read date from input:
<input type="date" id="myDate" >
and by a listener I run myDate function, it produce sql1 that is a string, it should pass sql1 to $sql, I tried that users explain in other Q's but I don't get the right answer :
<script>
var dateBut = document.getElementById("myDate")
function myDate() {
var x = document.getElementById("myDate").value;
document.getElementById("demo3").innerHTML = x;
// sql1 string
var sql1 = "SELECT * FROM TSE(" + document.getElementById("demo3").innerHTML +")";
document.getElementById("demo2").innerHTML = sql1;
var tbl = document.getElementById("demo");
tbl.innerHTML = "";
var ch = document.getElementById("checkerWork");
<?php
// sql1 should pass to $sql;
$sql = ?>"SELECT * FROM TSE(" + document.getElementById("demo3").innerHTML + ")"<?php
mysqli_set_charset($conn, "utf8");
$result = $conn->query($sql);
?>
}
dateBut.addEventListener("change", myDate, false );
all of the code is in a php file.
thanks

I would send the contents of the variable to a PHP page via Ajax with jquery, then I would assign the data sent via get or post to the PHP variable, execute the query and return the result to the myDate () function. I'm not a native English speaker so if the answer is not clear tell me and I'll give you an example.

Related

How can I convert one value from int to string within an array [duplicate]

This question already has answers here:
Converting an integer to a string in PHP
(15 answers)
Closed 8 months ago.
enter image description hereI want to make my chart data.addColumn ('string', 'hour'); must be a string.
In my request it is an int.
$temp_array = array();
while($row = mysqli_fetch_assoc($result))
{
$temp_array = array(strval($row["hour"]), $row["min"], $row["max"], $row["Avg"]);
}
result now: "["6","30.0","30.0","30.0"]"
want only the first one("6") between quotes.
php code
javascript
Use the strval() to convert integer value to string. For example:
$temp_array[] = array( strval($row["hour"]), strval($row["min"]));
$temp_array = array();
while($row =mysqli_fetch_assoc($result))
{
$temp_array[] = array( (string)$row["hour"], $row["min"], $row["max"], $row["Avg"]);
}
strval($row["hour"]) should do the trick.

How to pass a String variable from PHP to Javascript [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I'm trying to pass a string variable from PHP to Javascript and it is giving me troubles.
I actually pas a number variable as per the code below and it works, but once I introduce (or uncomment) the line var cityname = <?=$city;?>; in my javascript code then it doesn't work. I believe it has something to do with the type of data because if I introduce a number like Scity = 3 in my PHP then it works.
Thank you
I have the following PHP:
<?php
$get_total_rows = 0;
$city = "London";
$db = pg_connect("$db_host $db_name $db_username $db_password");
$query = "SELECT * FROM table";
$results = pg_query($query);
$get_total_rows = pg_numrows($results);
?>
and I have the following javascript:
<script type="text/javascript">
$(document).ready(function() {
var track_load = 0; //total loaded record group(s)
//var cityname = <?=$city;?>;
var total_rows = <?=$get_total_rows;?>;
$('#results').load("autoload_process.php", {'rows':total_rows}, function() {track_load++;}); //load first group
In the case of strings, the content must be between quotes (ex.: val = "content"). Try to do something like this:
var track_load = 0; //total loaded record group(s)
var cityname = "<?php echo $city; ?>"; // string
var total_rows = <?php echo $get_total_rows;?>; // number

inserting integer variable into Database

How can I put this int value in our database? I've tried putting (int) and intval before $_POST but it still doesn't work? Everything else works except the conversion(?) of that int value so it can be placed in our database. Did we miss anything in the code? Thank you in advance.
function computeScoregrammar(){
// code for computing the score here
aver = 5;
<?php
//db connection here
$avegs = $_POST['aver'];
$qidScores = 4;
//below part is not yet complete for we are only trying to update a sample data in the database
if($qidScores == 4){
$qs = "UPDATE scores SET GrammarScore = '$avegs' WHERE applicantID = '$qidScores'";
mysqli_query($conn,$qs);
mysqli_close($conn);
}
else {
//else statement here
}
?>
Try it:
$qs = "UPDATE scores SET GrammarScore = $avegs WHERE applicantID = '$qidScores'";
$avegs without using ''. If you use '', $avegs will be treated as string.
WARNING: SQL injection, do not put this online! Use PDO or escaping (mysqli_escape_string()) and remove the ''.

How to call a variable from other functions in JavaScript? [duplicate]

This question already has answers here:
Accessing variables from other functions without using global variables
(10 answers)
Closed 8 years ago.
I am trying to compute the total cost and using alert to display the total cost when the user hit the Submit button. I don't know how to call a var from other functions. Is there a way to do it? Any help will be appreciated. Thank you!
<script type = "text/javascript">
function CostCalculate(){
var TotalCost = AppleCost + OrangeCost + BananaCost;
alert("Your total cost is $ " + TotalCost);
}
function getQuantity1(){
var promptNum = prompt("Enter numbers of Apple: ");
var AppleNum = parseInt(promptNum);
var AppleCost = AppleNum * 0.59;
}
function getQuantity2(){
var promptNum = prompt("Enter numbers of Orange: ");
var OrangeNum = parseInt(promptNum);
var OrangeCost = OrangeNum * 0.49;
}
function getQuantity3(){
var promptNum = prompt("Enter numbers of Banana: ");
var BananaNum = parseInt(promptNum);
var BananaCost = BananaNum * 0.39;
}
Helo, I think you should read something about the variables scope in javascript.
https://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx
You can't acces a variable that's declarated in another function. What you can do is defining the variable outside of the functions.

PHP if condition not working [duplicate]

This question already has answers here:
boolean variable values in PHP to javascript implementation [duplicate]
(3 answers)
Closed 8 years ago.
I am having some problems with setting the value of "swimsuit" variable as defined below, however when I set the value to "1" in my MYSQL database, it sets $real=true but elsewise it says "real is undefined".
$query4 = "SELECT swimsuit FROM player_equipment WHERE player_name='xhyderz'";
$result = mysql_query($query4,$link);
$row = mysql_fetch_assoc($result);
$swim = $row['swimsuit'];
$real=null;
if($swim==0){
$real=false;
}else{
$real=true;
}
echo "<script type='text/javascript'> var swimsuit=".$real."; </script>";
When you concatenate a boolean variable with a string like that, you get the following:
var_dump(true . ""); // "1"
var_dump(false . ""); // ""
So if $real is false, your JavaScript looks like:
var swimsuit=;
You can either use the strings "true" or "false" for $real, or you can use json_encode:
echo "var swimsuit=" . json_encode($real) . ";";
Try :
if($swim==0){
$real="false";
}else{
$real="true";
}
Edit:
Look at cbuckley's explaination, way more clear and complete than mine.

Categories