How to convert file content to String in php - javascript

click here to see the errorI'm trying to convert a local php file into string to store it in a Javascript variable.
Is there any way to acheive this?
Here is my php code.
<?php
$data = get_file_contents("htmlFile.html");
echo $data;
>?
This data needs to be stored as string in Javascript variable.
I'm facing the below issue:
enter image description here
Please give your suggestions to achieve this.

Try this:
var data = "<? echo $data; ?>"
Remember using "" characters. That is what is generating erros.

Try This:
<?php
$data = get_file_contents("htmlFile.html");
?>
and use this script
<script>
var jsvar = "<?php echo $data; ?>"
</script>

Related

How to put php variable inside php variable with a javascript value

I need to put php variable inside php variable with javascript value, here is some of my codes that I tried but doesnt work (its file is in .php):
$rowEmail = "EmailTest";
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('"echo $rowEmail;"');</script>";
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".echo $rowEmail."');</script>";
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".$rowEmail."');</script>";
You have some misunderstanding on how to output data via PHP. Need something like:
$rowEmail = "EmailTest";
$script = "<script type='text/javascript'>console.log('". $rowEmail ."');</script>";
echo $script;
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('" . $rowEmail . "');</script>";
You don't need the echos, just concatanate the string.
If it is not a must for the love of what's good in this world do not store tags in variables like that. Makes it incredibly hard to read and debug later on.
Your last line is correct I don't know you why say it doesn't work,
I've tried it and it works :
<?php
$rowEmail = "EmailTest";
$script = "<script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('".$rowEmail."');</script>";
echo $script;
// displays <script type='text/javascript'>$('#reminderModal').modal('toggle'); console.log('EmailTest');</script>

Sending a PHP session variable to javascript of another file

I extracted data from a CSV file using PHP and stored the data as a nested array.
I stored this array as a session variable and now I want to access the array in another html file using javascript. How can this be done?
<?php
session_start();
$arr=array();
$arr['1']=array('abc','def');
$arr['2']=array('123','456');
$_SESSION["abc"]=$arr;
?>
This is a sample php. I have to access the session variable in javascript.
Yes, you can use implode PHP function to convert your array to string and then echo that string and assign to a JS variable like in example below.
Your another HTML file:
<?php
session_start();
$your_array=$_SESSION['your_array'];
?>
<script>
var js_variable = [<?php echo implode($your_array,','); ?>];
console.log(js_variable);
</script>
What you should be aware of, is that php is server side and javascript is client side. So to solve this, you have 2 options:
let the php code add this variables in the head. on to of everything:
<script>
var php_variables= <?php echo json_encode($your_php_variables); ?>;
</script>
or 2. Make a seperate php file which just echos only the value in json and put a json header:
<?php
header("content-type:application/json");
echo json_encode($your_php_variables);
?>
and then let javascript retrieve it. there are many libraries available for that. but lets take a comon one like jquery:
$.get("yourphpfile.php",function(data){do something here})

Redirect a page with PHP and send a JSON string to Javascript

Right now I have a php file that recieves a string from a search bar on page1 of my website. It queries the data bases and creates a JSON string of results. I can then use PHP to redirect my website to page2 (The search results page). How can I receive that JSON string in the Javascript of page2?
First you need a PHP file that will encode json code . Make a file like this:
http://localhost/project/myjs.php
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
Then your JavaScript (in this example I use jQuery):
$.getJSON('http://localhost/project/myjs.php', function(data) {
console.log(data);
});
This should be a start to get PHP arrays in to your JavaScript.
To redirect browser to another page you have to use header function
header:('Location: http://example.com/page2.php');
You can store data in $_SESSION array between pages like that:
page1.php
session_start();
$_SESSION['array'] = array('foo'=>$bar);
page2.php
session_start();
echo json_encode($_SESSION['array']);
page1.php:
<?php
$my_array = array('foo'=>$bar);
$json_data = json_encode($my_array);
header:('Location: http://example.com/page2.php?data='.$json_data);
?>
page2.php
<?php
$data_get = $_REQUEST['data'];
?>
<script type="text/javascript">
var mydata =<?php echo $data_get; ?>;
</script>

how to get php data at javascript variable

This is a php file below named getques.php at server. In this php file, the variables subnm1, chpno1 and qnumber1 are posted from a html-javascript file from client side. With these, a database at server is accessed and the output of SQL query is stored in a variable $px.
<!DOCTYPE html>
<html>
<body>
<?php
$x = $_POST["subnm1"];
$y = $_POST["chpno1"];
$z = $_POST["qnumber1"];
$con=mysqli_connect("localhost","compete7_bhunia","pr393ss","compete7_iitmock");
if (mysqli_connect_errno())
{ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$result = mysqli_query($con,"SELECT question, optiona, optionb, optionc, optiond,
coroption FROM $x WHERE qnumber = $z AND chapno = $y");
while($row = mysqli_fetch_array($result))
{
$px = $row['question'];
}
mysqli_close($con);
?>
</body>
</html>
This $px is showing its desired data that has been retrieved from database. Now, I want that this variable $px should be passed to the same html-javascript file. How to get it at javascript. The relevant part is as below.
<script type="text/javascript">
var abc = "<?=echo $px ?>";
alert(abc);
</script>
This is not showing the value of $px. Pl. help.
Avoid using short open tags.May be in your php short open tags is not enabled. So try like this..
var abc = "<?php echo $px ?>";
short_open_tag directive) with PHP 5.3 or below (but since PHP 5.4.0,
Actually, in the php.ini-production file provided with PHP 5.3.0, they are disabled by default:
$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = Off
So, using them in an application you want to distribute might not be a good idea: your application will not work if they are not enabled.
Use like this :
<?php echo $px ?>";
As others have stated, you have a syntax error, your output line should read like
var abc = "<?php echo $px ?>";
However, this is not safe (and don't use short open tags <?=, as they may be disabled in PHP < 5.4): consider that $px contains a double quote, then your syntax will be malformed; it is even possible to inject JavaScript code this way. Don't do this!
A safer method is to use json_encode:
var abc = <?php echo json_encode($px) ?>;
(Note that you don't need to surround this with quotes.)
On a side note, you are vulnerable to SQL injection. Please use prepared statements to prevent this.

pass parameters using variable?

This is a simple syntax question.
I declare a variable:
<script type="text/javascript">
var id_1= '<?php echo $id; ?>';
</script>
And then in a externally loaded js file im trying to call a function using the variable (the external js file is loaded after the ^^ variable declaration:
loadComments(id_1);
The id_1 is being passed literally as 'id_1', not recognizing it should be a variable. What am I doing wrong?
var id_1 = '<?php echo $id; ?>';
Will echo something like this:
var id_1 = '10';
Which is treated as a string in JavaScript. You want to do this instead, so that you assign a number to id_1:
var id_1 = <?php echo $id; ?>;
This will print out something like this:
var id_1 = 10;
The php code is recognized only by a .php file.
Put your code in a .php file and run it on you local server

Categories

Resources