I am trying to send the contents of javascript file using file_get_contents. The problem i'm having is the javascript contains some PHP variables:
<script>
jQuery('document').ready(function($){
$('a.ajax_link').click(function(){
$.ajax({
type: 'DELETE',
url: '/groups/$org_ID/leave/$member_ID',
success: function(data){
var data = $('<div>').html(data);
var msg1 = data.find('#msg1');
$('#data_box1').html(msg1).hide().fadeIn(500).delay(2000).fadeOut(500);
setTimeout('window.location.href =\"/groups/\"',3000);
}
});
});
});
</script>
Specifically, the url: line. That just stays as plain text and so my script fails. I now understand this is the behavior of file_get_contents but how can I go about doing this?
thanks!
Use output buffering and require:
$org_ID = 5;
$member_ID = 10;
ob_start();
require '/path/to/javascript_file.php';
$js_code = ob_get_clean();
Inside javascript file you'll have to use php tags:
url: '<?php echo "/groups/$org_ID/leave/$member_ID"; ?>'
Related
My AJAX call previously returned an array of data as JSON using "echo jason_encode(array)" in my PHP code and I then looped through the result in my script and built the HTML using the returned data before displaying.
Now I changed the code to build the HTML in my PHP code and I want to return the HTML string to my script and display the HTML but I have no idea how to get it working and I have looked at over a dozen examples on this site and others but no luck.
PHP
$html = '<tr><td><div class="dummy">This is some text.</div></td></tr>';
$arr[] = array('html' => $html);
echo json_encode($arr);
Script
<script type="text/javascript">
$('.mashed_row a').click(function () {
var link_id = $(this).attr('link_id');
$.ajax({
type: 'POST',
url: 'explode',
data: {'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>', link_id},
dataType: 'json',
success : function(data) {
if(data)
{
var txt = data['html'];
$("#xarticletab").html("");
$("#xarticletab").append(txt).removeClass("hidden");
$("#comments").removeClass("hidden");
}
}
});
return false;
});
</script>
First of all, I think there is an error on your JS code: link_id alone.
on JS, you should define key: value. So, maybe 'link_id': link_id ?
Correct typo: {key1: 'value1', 'key2': 'value2'}
After, use console editor of your browser to know if there is any Javascript error.
Alternatively, you can play with javascript functions: console.log(data); or event alert(data)
I need pass jQuery variable to PHP variable, so I made simple code to test and it doesn't work.
It's only a testing files. Finally, I have to do something more advanced but i can't make it works!
I have 3 files
In character.html I have:
SAVE
and in character.js (it's external javascript for character.html)
$(document).ready(function() {
$('.saved').click( function () {
var avatarID = 123;
$ajax({
url :'character-save.php',
data:{"avatarID":avatarID},
type: 'post',
dataType: 'json',
});
});
});
in character-save.php i try this:
<?php
header('Content-type: application/json');
$result = $_POST['avatarID'];
$result = htmlentities($result, UTF-8);
echo json_encode($result);
exit();
?>
And it doesn't print 123
In your php file, you have a mistaks with UTF-8, it should be included with parentheses.
<?php
header('Content-type: application/json');
$result = $_POST['avatarID'];
$result = htmlentities($result, 'UTF-8'); // It should be 'UTF-8'
echo json_encode($result);
exit();
?>
Your syntax is wrong its not $ajax its $.ajax don't forget the . dot.
Also you need some way of checking response so either update your html by adding ajax callback and necessary jquery, use alert or log reponse to console. Something like this should give you a good indication.
$.ajax({
url :'character-save.php',
data:{"avatarID":avatarID},
type: 'post',
dataType: 'json'
}).done(function(response) {
alert(response.data);
});
In your PHP change $result = htmlentities($result, UTF-8); to $result = htmlentities($result); also validate your json by putting result in an array then encode that array as json and echo it like this:
<?php
header('Content-type: application/json');
$result = $_POST['avatarID'];
$result = htmlentities($result);
$return["data"] = $result;
echo json_encode($return);
exit();
?>
In your javascript you have to add e.preventDefault(); to prevent the page redirecting to character-save.php
And inside the click function add e
see the updated javascript section below
$(document).ready(function() {
$('.saved').click( function (e) {
e.preventDefault();
var avatarID = 123;
$.ajax({
url :'character-save.php',
data:{"avatarID":avatarID},
type: 'post',
dataType: 'json',
success: function(msg){
alert(msg);
}
});
});
});
Try this,
$(document).ready(function() {
$('.saved').click( function () {
var avatarID = 123;
$ajax({
type:"POST",
url: 'character-save.php&avatarID='+avatarID ,
success:function(response){
alert(response);
});
});
});
In character-save.php try this:
<?php echo $_GET['avatarID'];
?>
I just want to pass my variable data.company_name from callback function:
<script>
function AjaxInsert() {
var company_name = $("#company_name").val();
dataToSend = {};
dataToSend.company_name = company_name;
$.ajax({
url: 'insert.php',
data: dataToSend,
dataType: "json",
success : function(data) {
console.log("my variable: "+data.company_name);
}
});//ajax*/
}
</script>
,below in the same script into a PHP (PHPExcel) method:
<?php
$var_needed = 'I need that data.company_name here';
$objWorksheet->setCellValueByColumnAndRow(0,$num_rows+1,$var_needed);
?>
How can be this achieved?
The "same script" has been executed already, so you won't be able to interact with it anymore after page load.
You either want to manipulate your spreadsheet client side with a JS library, or to drop ajax and reload the whole page [and ruin your user experience by the way].
<?php
$var_needed = $_REQUEST['company_name'];
$objWorksheet->setCellValueByColumnAndRow(0,$num_rows+1,$var_needed);
?>
$objWorksheet->setCellValueByColumnAndRow(0,$num_rows+1,$_POST['company_name']);
How can I load my php on my domain using my javascript that was loaded dynamically.
my sample code
sample.html(Client-side)
<div onClick = "getThis()"></div>
my.js(Client-side)
function getThis(){
var url = "http://www.sampledomain.com/test.js"
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
}
test.js(Server-side)
$.get("index.php", function(data) {
alert(data);
});
index.php(Server-side)
<?php
$_SESSION['user'] = "rob098";
?>
<script>
var data = '<?php echo json_encode($_SESSION['user']) ?>';
</script>
but it doesn't alert any.
Use ajax and json:
sample.html
<div onClick = "getThis()"></div>
my.js (inside sample.html)
function getThis() {
$.ajax({
url: '/test.php',
dataType: 'json',
success: function(data){
alert(data.user); //should alert rob098
},
});
}
test.php
header('Content-type: application/json');
echo json_encode(array("user" => "rob098"));
you have to append your js file to this
$(document).ready(function(){
$.get("index.php", function(data) {
alert(data);
});
});
so when script file loaded it will execute code inside jQuery ready function
I think you are using jquery $.get() the wrong way. Well I've never seen used that way at least, so you need to change your index.php to this:
<?php
$_SESSION['user'] = "rob098";
echo json_encode($_SESSION['user']);
header('Content-type: application/json');
?>
Let me know if I helped.
I'm trying to make the URL in the following example contain a php variable like this:
http://api.crunchbase.com/v/1/company/$company.js
This is my jquery code:
$.ajax({
url: "http://api.crunchbase.com/v/1/company/airbnb.js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}
What do I need to do to achieve this?
Thanks!
You'll need to output the variable as JavaScript from PHP at some point in the script. This can be done quite easily using json_encode:
var company = <?php echo json_encode($company) ?>;
Note that this will have to be placed in a file that is actually executed by PHP, rather than an inert .js file. One way to achieve this is to put it in an inline <script> in the HTML in your PHP template; e.g.
<script type="text/javascript">
var company = <?php echo json_encode($company) ?>;
</script>
Try this:
<script type="text/javascript">
var company = <?php echo json_encode($company); ?>;
alert(company);
</script>
You're setting the company variable with the output of $company, you can then use this variable however you please in JavaScript.
$.ajax({
url: "http://api.crunchbase.com/v/1/company/" + company + ".js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}
Can you try this, assuming you have the company name in the php $company variable:
$.ajax({
url: "http://api.crunchbase.com/v/1/company/<?php echo $company ?>.js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}