In this program similar_text function do not work but echo successfully print $var_1 and $var_2. What is the exact error?
<script>
var j=prompt('1st name','Name')
var l=prompt('2nd name','Name')
</script>
<?php
$var_1 = '<script>document.write(j)</script>';
$var_2 = '<script>document.write(l)</script>';
similar_text($var_1, $var_2, $percent);
echo $var_1, $var_2;
echo $percent;
?>
PHP is executed first, on the server, then it gets served and then only javascript is executed on the client-side. so the variables you are using in php part are not set at that moment.
If you need to interact with a php script from javascript, you odd to use an ajax request.
you need close php and open again
<script>
var j=prompt('1st name','Name')
var l=prompt('2nd name','Name')
</script>
<?php
$var_1 = '<script>document.write(?>j<?php)</script>';
$var_2 = '<script>document.write(?>l<?php)</script>';
similar_text($var_1, $var_2, $percent);
echo $var_1, $var_2;
echo $percent;
?>
or same.
Related
I have a php page that loads a JSON object string from a text file. I want to send the object string to an external javascript file which will eventually use it to update html displayed from the php page. Unfortunately I've had trouble getting the string to the external javascript.
I've been trying to follow the approach outlined by Afzal Ahmad here
Pass Php Arrays to External Javascript File
but I get no results
The php:
<?php
session_start();
echo 'Hello ' . $_SESSION['first'] . '<br>';
loadUserData();
displayPage();
function loadUserData(){
$userString = 'userdata/'.$_SESSION['email'].'.txt';
echo $userString;
$user = file_get_contents($userString);
}
function displayPage(){
/*html stuff here*/
}
?>
<script type="text/javascript">var userObj = <?php echo json_encode($user); ?>;</script>
<script type="text/javascript" src="scripts/index.js"></script>
The javascript:
console.log(userObj);
Your loadUserData function isn't returning anything.
You should remove the echo $userString; and add a return $user after the file_get_contents.
And you should change the loadUserData(); to $user = loadUserData();
That happens because you haven't declared $user in the function loadUserData as a global variable.
To fix the issue, you'll have to use the global keyword:
function loadUserData() {
global $user;
$userString = 'userdata/'.$_SESSION['email'].'.txt';
echo $userString;
$user = file_get_contents($userString);
}
I'm new at PHP and I don't understand why this happens.I try using echo to show "$imglinksis" and the result is exactly http://catpic.s3.amazonaws.com/product.jpg
I do not understand why the 2nd Code fails. Please help!
Code #1: This code completely works in returning the fields I want
<?php function CallCatpicAPI($photoUrl){do something...}
$imglinksis = "http://catpic.s3.amazonaws.com/product.jpg";
$jsonReturnCatpic = CallCatpicAPI($imglinksis);?>
Code #2: Fail to return: API says invalid URL image link
<?php function CallCatpicAPI($photoUrl){do something...}?>
<script>var img_link = "http://catpic.s3.amazonaws.com/product.jpg";</script>
<?php
$imglinksis = "<script>document.write(img_link).toString()</script>";
$jsonReturnCatpic = CallCatpicAPI($imglinksis);?>
You are mixing the two codes. Your php is executed on the server but the javascript is executed on the client side after php executed. You can fix your second code by this:
<?php
$imglinksis = 'http://catpic.s3.amazonaws.com/product.jpg';
function CallCatpicAPI($photoUrl){do something...}
?>
<script>var img_link = '<?php echo $imglinksis; ?>';</script>
<?php
$jsonReturnCatpic = CallCatpicAPI($imglinksis);
?>
HTML:
<section class="clientbox">
<div class="container">
<div class="col-lg-10 col-lg-offset-1">
<h2>WHAT CLIENT SAYS</h2>
<h4>POSITIVE REVIEWS FOR LOVING PROBLAM.COM </h4>
<p id="testimonial"><?php echo $alldata[0]['text'] ?></p>
</div>
</div>
</section>
javascript:
<script type="text/javascript">
<?php $i = 1; ?>
setInterval(function()
{
document.getElementById('testimonial').innerHTML = "<?php echo $alldata[$i]['text'] ?>";
<?php $i++; ?>
}, 3000);
What actually I am trying to do is change the text of 'testimonial' from values stored in $alldata array every 3 secs.
The problem is that the php variable $i is not getting updated. It stays 1 only.
You mixed the server side logic (PHP) with the client side logic (JavaScript).
When the client request your web page, PHP render the contents which contains HTML, CSS, JavaScript and some other contents and the web server send these contents to the client side.
The web browser receives these contents and then execute the JavaScript and render the HTML according to the styles.
You need to generate all the contents needed by the JavaScript setInterval on the server side, and adjust your JavaScript logic to iterate on these data.
The logic may looks like the following. I'm not familiar with PHP, not sure if this is valid. The JavaScript doesn't check if all data has been consumed.
<script type="text/javascript">
<?php
function get_data($e) {
return($e['text']);
}
$data = array_map("get_data", $alldata);
?>
var js_alldata = <?php echo json_encode($data) ?>;
var i = 1;
setInterval(function()
{
document.getElementById('testimonial').innerHTML = js_alldata[i % js_alldata.length];
i++;
}, 3000);
You can't mix PHP and Javascript like that. PHP runs first, producing some data for Javascript to interpret. From Javascript's perspective, it can only see this:
<script type="text/javascript">
setInterval(function()
{
document.getElementById('testimonial').innerHTML = "some data";
}, 3000);
</script>
You would need to pass all of the data to Javascript so it can iterate over it.
See this answer for how to mix PHP and Javascript.
I've made this code and it is working. As people above told you, server side cannot work with the client-side iterations or setIntervals, therefore you should send the whole data beforehand.
Example:
$array = array();
$array[0]['text'] = 'hi';
$array[1]['text'] = 'hey';
$array[2]['text'] = 'hou';
$js_array = json_encode($array);
?>
<p id="testimonial"><?php echo $array[0]['text'] ?></p>
<script>
var json;
var i=1;
json = <?php echo $js_array; ?>
setInterval(function()
{
document.getElementById('testimonial').innerHTML = json[i]['text'];
i++;
}, 3000);
console.log(json);
</script>
I have a php varaiable that I need to pass into a link in a javascript function
//In the config
<?php
define('THE_VAR', 'test');
?>
//On the page
<?php
$the_var = THE_VAR;
?>
<script>
(function()){
link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"
})();
</script>
I need the link in the javascript function to read.
test.site/test/testing.js
Instead of: link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"
Use: link = "test.site/<?= $the_var; ?>/testing.js";
Use Ajax for passing the data from php into the javascript.
http://www.w3schools.com/ajax/ajax_php.asp
<script language="javascript">
function emergency()
{
if(document.getElementById('emer').checked == true)
{
<?php $_SESSION['total2']= $_SESSION['total1'] + 50; ?>
document.getElementById('emr').innerHTML = "<b>Payable Amount: </b>" + <?php echo $_SESSION['total2']; ?>;
}
if(document.getElementById('emer').checked == false)
{
<?php $_SESSION['total2']= $_SESSION['total2'] - 50; ?>
document.getElementById('emr').innerHTML = "<b>Payable Amount: </b>" + <?php echo $_SESSION['total2']; ?>;
}
}
</script>
here I am adding $_SESSION['total1']+50 and put the value in new session SESSION['total2']. The problem is that , when I echo this new $_SESSION['total2'] out side script,ie, in the page like
<?php echo $_SESSION['total2'];?>
it returns the value of $_SESSION['total1']).
Are you calling a javascript function to change value of php session variable?..
this could not be possible because, php is a server side language and javascript is client side so php code gets executed when the page is loaded..
so if you want to do this on javascript function call,, than you need to use ajax...
to get the new value of $_SESSION['total2'] you have to run the code on server side again. that means before getting the new value for total2 session variable you have to reload the appropriate php file.