Value is not transferring with GET method - javascript

Whenever I write echo $_GET['sec']; then it shows the value of sec
but when I try the following code:
$(document).ready(function() {
setInterval(function () {
$('#div_id').load('../data.php?id_to=<?php $_GET['sec'];?>')
}, 100);
});
The value of "sec", which is coming from another page does not transfer to
data.php with id_to variable.
What's wrong with my code?
I can see the value of $_GET['sec']; in current page but the value is not available on the data.php file.

Code is okay so variable if available should be passed
So what can you try is:
You have simple mistake you don't echo
change <?php $_GET['sec'];?> to <?php echo $_GET['sec'];?> or <?= $_GET['sec'] ?>
also:
what's the output of this php code is it ile '../data.php?id_to=VALUE' If output is okay then variable will be passed via GET method
In your data.php try doing echo $_GET['id_to'] maybe you try to output sec and this causes problem?
You can always try print_r($_GET); on data.php

You're not outputting the GET variable into your JS string.
Replace:
$('#div_id').load('../data.php?id_to=<?php $_GET['sec'];?>')
With:
$('#div_id').load('../data.php?id_to=<?php echo $_GET['sec'];?>')
// That was missing ^
Alternatively, there's a shorthand available for a php echo:
$('#div_id').load('../data.php?id_to=<?= $_GET['sec'];?>')
While the syntax highlighter on here may make it seem like the nested single quotes will interfere, they won't. <?= $_GET['sec'];?> will literally be replaced by the value in sec, resulting in a valid JS string.

Related

How minify javascript function receiving PHP Arrays?

I have a javascript function receiving php arrays like this :
let options = <?php echo $arrayoptions; ?>;
let optlibs = <?php echo $arrayoptlib; ?>;
let forms = <?php echo $arrayforms; ?>;
It works well but problem arises when I try to minify function. All minifiers I tried gave en error with php part. What can I do ?
Yes, you will run into errors because PHP is not part of Javascript syntax.
Even your Javascript part for the arrays seems dynamically generated by PHP.
This will be impossible to handle.
The only way to work around this would be modify your javascript to fetch those arrays and store them somewhere your whole script can access them for later use.
Then, the script can be minified without issue since PHP syntax are no longer part of it.

Jquery if/else statement with dependencies on php session variable [duplicate]

I have a PHP page with some JavaScript code also, but this JavaScript code below doesn't seem to work, or maybe I'm way off!
I am trying something like this:
var areaOption=document.getElementById("<?php echo #$_POST['annonsera_name']?>");
areaOption.selected=true;
Also I have tried this, but it only alerts a BLANK alert-box:
alert (<?php echo $test;?>); // I have tried this with quotes, double-quotes, etc... no luck
Am I thinking completely wrong here?
UPDATE
Some PHP code:
<?php
$test = "Hello World!";
?>
In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).
Test this:
alert (<?php echo "'H'";?>);
OR
alert ('<?php echo "H";?>');
PHP runs on the server side and Javascript is running on the client side.
The process is that PHP generates the Javascript that will be executed on the client side.
You should be able to check the JS that is generated just looking at the code. Of course, if the JS relies on some PHP variables, they need to be instanciated before the JS is output.
<?php
$test = 'Hello world';
?>
<html>
<body>
<script>
alert('<?php echo $test; ?>');
</script>
</body>
</html>
will work but
<html>
<body>
<script>
alert('<?php echo $test; ?>');
</script>
</body>
</html>
<?php
$test = 'Hello world';
?>
will not
Use json_encode to convert some text (or any other datatype) to a JavaScript literal. Don't just put quotes around the echoed string — what if the string has a quote in it, or a newline, or backslash? Best case your code fails, worst case you've got a big old cross-site-scripting security hole.
So,
<?php
function js($o) {
echo json_encode($o, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
}
?>
<script type="text/javascript">
var areaOption= document.getElementById(<?php js($_POST['annonsera_name']); ?>);
areaOption.selected= true;
alert (<?php js('Hello World'); ?>);
</script>
Your using #$_POST indicates that you have received (or are expecting) errors - check your generated source to see if the value was output correctly. Otherwise document.getElementById will fail and you'd get no output.
alert("Delete entry <? echo $row['id']; ?> ")
If your extension is js, php will not work in that file.
The reason being, php parses on files that it is supposed to. The file types that php will parse are configured in httpd.conf using AddType commands (or directives, whatever they are called).
So you have 3 options:
add filetype js to the list of files php will parse (BAD, VERY BAD)
make the script inline to some php file
rename the file to script.js.php, and at the beginning of the file, specify the content type, like so:
<?php header( 'content-type: text/javascript' ); ?>
Cheers!

Can I create JavaScript function in PHP looping

I want to make an alert using JavaScript. This is an example I have made bls.hol.es.
When I click first name will display an alert containing the first name. But it displays an alert containing the second name.
This is my code:
<?php
$arrayname=array('iqbal', 'rezza');
$arrayaddress=array('teloyo', 'karang rejo');
for ($x = 0; $x <= 1; $x++)
{
$name=$arrayname[$x];
$address=$arrayaddress[$x]; // indent consistently!!
?>
<script>
function call()
{
var nik = "<?php echo $name ?>"; // semicolons not needed in the embedded PHP code
var addres = "<?php echo $address ?>"; // indent consistently!!
alert(nik+address);
}
</script>
<?php echo $name ?>
<?php
} ?>
Whether this can be done my way?
What is wrong in my code?
That happens because you're creating js function call() inside the loop, so technically you have two declarations of call() in resulting code. When you call the call() function, then both of them are processed.
I assume you want to use more logic in the call() in the future, so the solution would be to alter your js code to process the name as a parameter nad move it outside the loop like:
function call(name){
alert(name);
}
and alter the link:
<?php echo $name;?>
What's wrong with your code is the second pass through the loop redefines the call() function with the second name, so by the time it's been rendered in the browser window, only the second instance of the function is available to run.
Just loop the html with inline onclick using the php loop.
<a onclick="alert(this.innerHTML);">iqbal</a>
<a onclick="alert(this.innerHTML);">rezza</a>
Here's the php:
$arrayname=array('iqbal', 'rezza');
foreach($arrayname as $v){
echo "<a onclick='alert(this.innerHTML);'>$v</a>";
// or echo "<a onclick='alert($v);'>$v</a>";
}
The bottom line is, you don't want to write duplicate functions. Write one function, call it as many times as you wish, and pass $v as a parameter to it, then you can do what ever you wish with the parameter inside the function.
If you are making a function call and want to send three variables at time, you can set it up like this in php:
<script>
function call(one,two,three){
alert(one);
alert(two);
alert(three);
}
</script>
<?php
$array=[
['a','b','c'],
['d','e','f']
];
foreach($array as $a){
echo "{$a[0]} and {$a[1]} and {$a[2]}";
}

how to hold javascript variable in php variable

I want to use javascript variable as php variable. I am echo php variable then its print. but when i am use for fetching data from database its show an error
Notice: Undefined index: document.write(i)
here my code
javascript
var i=0;
function inc()
{
i+=1;
}
<?php $foo="<script>document.write(i)</script>"; ?>
php
code work for
echo $foo
code not work for
$i=$foo;
$query="select * from TABLE where id = $i";
$result=mysqli_query($conn,$query);
while($row=mysqli_fetch_row($result))
{
echo $row[0];
}
Then It show This Error Notice: Undefined index: document.write(i)
PHP is server-side code that is run to generate a page. Javascript is client-side code that is run after the page is sent to the visitor's browser. Javascript can't affect the server-side code because the server code is done running by the time the Javascript runs. If you want to have a user's selection change the behavior of the PHP code the next time the form is loaded, pass a variable through a $_POST variable when the form is submitted.
If you want your PHP and Javascript code to be using the same value, have the PHP code write a Javascript variable initialization into the page's <head> section before any Javascript would run that would need to use it.
<script>
var i=0;
function inc()
{
i+=1;
return i;
}
</script>
<?php
$foo = '<script type="text/javascript">document.write(inc());</script>'; //Script function call which return the var i value to php variable
echo $foo;
?>

How to get PHP string value in javascript?

Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;

Categories

Resources