<script type="text/javascript">
var id=<?php print''.entry1->id.''?>;
var dataString = 'id='+ id;
$.ajax({
type: "POST",
url: "ajax_unfollow.php",
data: dataString,
cache: false,
});
</script>
there have a error in this line
var id=<?php print''.entry1->id.''?>;
how can i write php code in var id?
this javascript run in a foreach loop.
write like this :
var id= '<?php echo entry1->id;?>';
You can try this
var id = '<?=$entry1->id?>';
You should add a single quotes around your PHP expression.
So, the corrected code should be:
var id='<?php print''.entry1->id.''?>';
Also, another thing:
You should avoid short_open_tag.
So, please use <?php echo $variable;?> instead of <?=$variable;?>.
Because, most of the PHP running servers should have short_open_tags directive off.
So, your PHP code, in this case will be considered as a plain text and will be shown to user.
Without it, whatever generated by the PHP should be considered as a JavaScript variable.
Which should cause an error.
since value of print should be string so you can try this code.
var id="<?php print''.entry1->id.''?>";
Related
I am new in PHP, i want to use "base_url"(php variable) in javascript file,But right now i am unable to load image,Here is my current code,How can i do this ? Thanks in advance.
$('#site-logo').find('img').attr( {src:'<?php echo base_url(); ?>assets/images/logo/logo_dark#2x.png',width:'151',height:'45'} );
You can create a variable or a constant in the javascript and store the base url there and use the same in the code.
Something along the line of following
var BASE_URL = "<?php echo base_url(); ?>";
$('#site-logo').find('img').attr({src: BASE_URL+'/assets/images/logo/logo_dark#2x.png',width:'151',height:'45'});
I have an HTML input with a function and parmeter set to it like so
<input onclick='myFunc($count)' type='button' value='ClickMe'>;
I also have script references to JQuery and my script file
<script src="jquery.js"></script>
<script src="myscript.js"></script>
Inside my myscript.js file I have the contents as such
function myFunc(x) {
alert(x);
$(document).ready(function() {
$.ajax({
url: "myphp.php",
method: "post",
data: { param1: "x" },
dataType: "text",
success: function(strMessage) {
}
})
})
}
Here is my myphp.php file
<?php
$param1 = $_GET['param1'];
echo $param1;
?>
As you can see in the javascript file, I have alert(x); to test that the function is grabbing the $count variable and so far that is working (the correct value appears in the alert box). However, I want to pass that x parameter to the PHP script but my PHP script won't echo anything, I assume the line where I have param1 is wrong. Any tips?
In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:
<?php
$param1 = $_POST['param1'];
echo $param1;
?>
You're making the XHR with POST method
method: "post",
and youre looking for it in the GET array
$_GET['param1'];
Change either to post or get (keeping in mind your scenario) and you should be good imo.
read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html
You are using post method in AJAX but trying to grab the value in $_GET which is wrong.
In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.
or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
https://www.w3schools.com/jquery/jquery_ajax_get_post.asp
I would need to echo php variables (from the server and related to service behaviour) into javascript. For example, "ssl" => "true" and adapt accordingly in javascript. The thing is I'm doing this for some API files I'm writing and I want the client to have direct access to these files (<script src="... .js">) and this forces me to hardcode info that might change in the future across multiple file references. What I'd like to do was something like this, is it possible? (the content to fetch must remain completely private - from the server folders to the php script files - and so it is really not an option to fetch this info using javascript):
api.js
<? //PHP here. (I know...)
(...)
?>
//some javascript
var is_secure = <? echo "true"/"false" ?>
(...)
So that when doing <script src="api.js"/> the user only fetches:
var is_secure = "true";
(...)
or
var is_secure = "false";
(...)
Any idea on how I could do something similar? Thank you very much...
You could just create your PHP file with this line before everything :
header("Content-Type: application/javascript");
EDIT:
I did misunderstood the question. If you want js in an php file, you should do it like this:
header("Content-Type: application/javascript");
OLD ANSWER:
I don't know if you know, but client can't see your php code...
So if You'd:
echo "Something"
The client would only see Something.
It's not clear, what you're trying to do...
If you don't want to see Something when you go directly into your "secret" php page, then you should do it like this:
JS:
jQuery.ajax({
type: "POST",
url: 'secretfile.php',
dataType: 'html',
data: {apiRequest: 1},
success: function (response) {
yourVar = response;
}
});
PHP:
If ($_POST['apiRequest']==1){
echo "Something";
}
yourVar would be = "Something"
If you'd go to this page, it wouldn't display anything;
I've seen many posts related to what I'm asking, but I can't seem to find an appropriate answer, and I'll admit I'm a n000b to both Javascript and PHP. I've followed some tutorials, but I can't seem to do what I want:
I have a HTML document that is subscribing to a SSE like so:
var eventSource = new EventSource("http://thewebsite.com/events/?access_token=" + accessToken);
I don't want to put the accessToken in the HTML page because it's insecure, so I'd like to hide the access token in a separate PHP file in the same directory, something like this:
code.php
<?php
$accessToken = "12345ABCDE";
?>
I can't figure out how to write the javascript to ask code.php for the accessToken.
echo should do the trick:
fetch.php
<?PHP
echo "12345ABCDE";
?>
but you need to use ajax to call the php:
$.post("fetch.php",function(data){access(data);},"text");
or
$.ajax({
type: "POST",
url: "fetch.php",
success: function(data){access(data);},
dataType: "text"
});
using token:
function access(incoming){
var eventSource = new EventSource("http://thewebsite.com/events/?access_token=" + incoming);
}
how safe it is to output your token from PHP is another story, but at least that's how it can be done.
According to jquery API this is the wat of sending value asigned to a name with ajax
.data( key, value )
Problem is my values are already in php variables. how can i send them using ajax?
this problem is relate to this question i asked yesterday. Still couldn't find a way to send php variables to another php page when a button is clocked. sad that jQuery API Documentation doesn't have examples.
You have two php....let say page.php and ajax.php. If you call ajax.php from ajax in page.php you must write variables from php to javascript with something like
var data1 = <?= $data1 ?>;
var data2 = <?= $data2 ?>;
and then add these variables to ajax call.
Best regards,
nele
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var ajax_value1 = $("#ajax_value1").val();
var ajax_value2 = $("#ajax_value2").val();
$.ajax({
type: "POST",
url: 'sample.php?ajax_value1='+value1+'&value2='+ajax_value2,
//Specify the datatype of response if necessary
data: $("#your_form_id").serialize(),
success: function(data){
}
});
</script>
<!-- Set your ajax value in html input fields -->
<input type="text" name="ajax_value1" id="ajax_value1" value="<?php echo $ajax_value1;?>" >
<input type="text" name="ajax_value2" id="ajax_value2" value="<?php echo $ajax_value2;?>" >