I am reviewing a web page that uses javascript, php, ajax and that with php5 worked correctly, but having updated to php7 no longer works.
I comment the details below:
In the screen I am reviewing, MyWebPage/Folder1/protected/views/applications/file1.php is executed which assigns to the variable $load this value:
$load='protected/views/aplicaciones/cargas/file2.php';
At the end of this php there is a block of code where it call several javascript libraries and then assign value to various dynamic variables, using this code:
<script type="text/javascript">
var startTime = "<?php echo $INIT; ?>";
var endTime= "<?php echo $END; ?>";
var period="<?php echo $PERIODUSED; ?>";
var db="<?php echo $DATABASE; ?>";
var loadpage="<?php echo $loadpage; ?>";
</script>
So this php calls the file3.js javascript using this code:
<script type="text/javascript" src="protected/views/aplicaciones/aplicacionesjs/file3.js"></script>
This javascript theoretically calls file2.php, using this code:
function requestData()
{
waitingDialog({});
$.ajax({
data: "startTime="+startTime+"&endTime="+endTime+"&period="+period+"&db="+db+"",
type: "POST",
url: loadpage,
datatype: "json",
success: function(data)
{
closeWaitingDialog();
var json= eval(data);
// Process and paint json
// ...
},
error: function()
{
closeWaitingDialog({});
},
cache: false
});
With Firebug I see that loadpage = protectec/views/aplicciones/cargas/file2.php
that looks to be right.
However, the problem is that after the line
var json= eval(data);
the value of json is undefined and instead of this it should contain a series of data that should be displayed on the page.
From the tests I have done with Firebug it seems to me that the problem could be that protectec/views/applications/loads/file2.php is not being executed.
Any comment or suggestion is appreciated.
Related
I call a variable (t_d_url) in a post function. It doesn't show the variable data. It shows only variable name.
I tried this way to call it:
$.post(t_d_url.'/ascott/includes/booking/booking-summary.php', {}, function(data) {
$(".booking-summary").html(data);
});
Here is the variable:
var t_d_url = '<?php echo get_template_directory_uri(); ?>';
Here is the output:
So I want to show this variable data in this function.
You can not use . to merge 2 strings. That only works in PHP. In Javascript you should use +..
$.post(t_d_url+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
$(".booking-summary").html(data);
})
You are using incorrect concatination operator. . doesn't work with Javascript try using + as
let url = "<?php echo get_template_directory_uri(); ?>/ascott/includes/booking/booking-summary.php";
$.post(url, {}, function(data){ $(".booking-summary").html(data);});
In worst case, this will call /ascott/includes/booking/booking-summary.php on current domain
You can use anyone of the following two method. Using + in JavaScript, because JS Doesn't merge strings using dot(.):
$.post(t_d_url+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
$(".booking-summary").html(data);
})
Using PHP tag
$.post('<?php echo get_template_directory_uri(); ?>'+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
$(".booking-summary").html(data);
})
Hello I am new to js and I stuck on the problem about passing variables to php via ajax.
<script>
$date = "123";
$.ajax({
url: './record.php',
type: "POST",
dataType:'text',
data: ({'date': date}),
success: function(data){
console.log("successfully");
}
});
</script>
And below is my code in record.php file.
<?php
session_start();
if(!empty($_POST['date'])){
//$hello = 'hh';
$_SESSION['date'] = $_POST['date'];
echo($_SESSION['date']);
}else{
echo "its empty(var)";
}
?>
Page always print out "its empty(var)" and I checked console, it shows "successfully"... I already stayed on this problem for several hours and looked for a lot of similar post, but I still can't find where is the problem. Can anyone have a look at it? I also already loaded the jQuery library.
you ar mixing js variable with php variable, js variables are declared with var and php variables are declared with $fieldname
<script>
var date = "123";
$.ajax({
url: './record.php',
type: "POST",
dataType:'text',
data: {'date': date},
success: function(data){
console.log("successfully");
}
});
</script>
if you want a PHP variable insite javascript you should define php variable first then asign it into js.
<?php $date = "123"; ?>';
<script>
var date=<?php echo $date; ?>';
$.ajax({
url: './record.php',
type: "POST",
dataType:'text',
data: ({'date': date}),
success: function(data){
console.log("successfully");
}
});
</script>
Hope it wil helps you.
You cant mix server (php) and client (js)
you might to this
<?php $var = 0?>
then in html you create
<input id="something" type="hidden" value="<?php echo $var?>">
then you get that value via
document.getElementById("something").value;
You are declaring $date and pass in your ajax as date.
Try this:
<script>
var date = "123";
$.ajax({
url: './record.php',
type: "POST",
dataType:'text',
data: {'date': date},
success: function(data){
console.log("successfully");
}
});
</script>
I am trying to set jQuery Ajax post URL.But it is not working as I want. I searched a lot and I got many solution.But none of them indicate my problem.
I set the base URL in var baseurl = "<?php print base_url(); ?>"; in js file and use it in $.ajax url concatenating it.It gives me
Disallowed Key Characters.php_print_base_url();_?>welcome/add_tag
I think the var baseurl is not working.
html
<script src="<?php echo base_url();?>js/my_js.js"></script>
<input type="text" id="add_tag" placeholder="add more tags"/>
js
var baseurl = "<?php print base_url(); ?>";
$('#add_tag').on('keyup paste', function () {
tag_text=$(this).val();
if(tag_text==='')
return;
$.ajax(
{
type: "POST",
url: baseurl+"welcome/add_tag",
data: {tag_textTo:tag_text},
success: function(data){
$('.tags_found').html(data);
}
});
});
CI_Controller
class Welcome extends CI_Controller {
public function add_tag()
{
$tag_text=$this->input->post('tag_textTo');
echo $tag_text;
}
}
How to make it work?thanks in advance.
You should declare the baseurl like this
var baseurl = '<?=base_url()?>';
or
var baseurl = "<?php echo base_url(); ?>";
And inside the jquery ajax call
url: baseurl+"welcome/add_tag",
Else totally
url: <?php echo base_url();?>"welcome/add_tag",
Note :
Don't forget to load the url helper
problem found
I am using an external js file for ajax call.
Now I know codeIgniter url helper does not recognize my baseurl variable in external js file.
That is line
var baseurl = "<?php print base_url(); ?>";in external file.
The answer given by #Sulthan Allaudeen and #saravanan n (thanks to them)all are working when I declare the baseurl var
in my php view file internally.
view file
<script type="text/javascript">var baseurl = "<?php print base_url(); ?>";</script>
The remaining js code can be stay on external file.
$('#add_tag').on('keyup paste', function () {
tag_text=$(this).val();
if(tag_text==='')
return;
$.ajax(
{
type: "POST",
url: baseurl+"welcome/add_tag",
data: {tag_textTo:tag_text},
success: function(data){
$('.tags_found').html(data);
}
});
});
Try with site_url
url: "<?php echo site_url('welcome/add_tag');?>"
JavaScript
function calcPrimesLoop() {
var primes = document.getElementById('primes');
primes.appendChild(document.createTextNode('\n'+this.prime.nextPrime()));
$.ajax({
url: "/test.php",
type: "post",
data: {prime: this.prime.nextPrime()},
success: function(data) {
}
});
calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}
Php
<?php
$content = $_POST['prime'];
$fn = "content.txt";
$content = stripslashes('prime'"\n");
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
?>
So this is all the relevant scripting I think. I have a script that can get prime numbers and it works perfectly. But now I want to record these numbers on a text file. This is how I am trying to do it but I am having no success at all. Thank you. The issue is the numbers aren't being recorded.
I added an alert the Ajax is working. But when I add a form to the php script and submit it that works. So the ajax and php scripts are not working together as such.
You should read up about AJAX and see how you can pass information to a serverside page using Javascript and retrieve the return value.
http://www.w3schools.com/ajax/default.asp
https://www.youtube.com/watch?v=qqRiDlm-SnY
With ajax and jQuery it is actually simple.
function calcPrimesLoop() {
var primes = document.getElementById('primes');
primes.appendChild(document.createTextNode('\n'+this.prime.nextPrime()));
$.ajax({
url: "myScript.php", // URL of your php script
type: "post",
data: {prime: this.prime.nextPrime()},
success: function(data) {
alert("success");
}
});
calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}
myScript.php :
<?php
$content = $_POST['prime'];
...
You should definately look for Asynchronous JavaScript and XML.
You can choose between using AJAX with a Javascript function, or simplify your life with jQuery
Here is a sample:
//STEP ONE: INCLUDE THE LAST VERSION OF JQUERY
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
//STEP TWO, GET YOUR FUNCTION TO WORK:
function sendVariableTo(variable,url) {
$.ajax({
url:url, //Or whatever.php
type: "GET", //OR POST
data: { myVar: variable}, //On php page, get it like $_REQUEST['myVar'];
success:function(result){
//If the request was ok, then...
alert(result) //Result variable is the php page
//output (If you echo "hello" this alert would give you hello..)
},
});
}
Hope this helped, bye !
So basically i have two files. 1 is my php file and it creates tables with some variables when it's called, and second file is jquery script file that makes that call. My script file:
$.ajax({
type: 'POST',
data: ({p:2,ank : ankieta,wybrane:wybrane}),
url: 'zestawienia_db.php',
success: function(data) {
$('#results').html(data);
}
});
and it works fine by printing my results.
My php file is echoing data that should be printed in my results div.
Question is how to get some PHP data variables and be able to use them in my jquery file without actually echoing them ??
Like i said in my comment to your question, a way to do that is by echoing the variables on a script tag, so you can access in javascript.
<script>
var PHPVariables;
PHPVariables.VariableName1 = '<?=$phpVariableName1?>';
PHPVariables.VariableName2 = '<?=$phpVariableName2?>';
PHPVariables.VariableName3 = '<?=$phpVariableName2?>';
</script>
And you could use those values accessing PHPVariables.VariableName1 on the javascript.
You can do this by echoing all the data you want like so peiceofdata§anotherpeice§onemorepeice§anotherpeice then you can use php's explode function and use § for the "exploding char" this will make an array of all the above data like this somedata[0] = peiceofdata somedata[1] = anotherpeice and so on.
the explode function is used like this
explode('§', $somestringofinfoyouwanttoturnintoanarray);
you can then echo the relevent data like so
echo data[0];
which in this case wiill echo the text peiceofdata.
write this type of code in ajax file
var data =array('name'=>'steve', date=>'18-3-2014');
echo jsonencode(data);
//ajax call in this manner
$.ajax({
type: 'POST',
data: pass data array,
url: ajaxfile url,
success: function(data) {
var data = $.parseJSON(data);
$('#name').html(data.name);
$('#date').html(data.date);
}
});
Use json format, and in this json add your data variables :
PHP :
$arr = array('var1' => $var1, 'var2' => $var2, 'var3' => $var3);
echo json_encode($arr);
Javascript :
$.ajax({
type: 'POST',
data: ({p:2,ank : ankieta,wybrane:wybrane}),
url: 'zestawienia_db.php',
success: function(data) {
data = JSON && JSON.parse(data) || $.parseJSON(data);
$('#results1').html(data.var1);
$('#results2').html(data.var2);
}
});