I have built a webform with Laravel and have it working the way I want it to on my localhost. However, when I put it on the server my form stops recognizing my php commands. For example see below.
In my working model, my code looks like this.This is using javascript to return an array of data elements that I need for my project.
var pageDownloadInfo=<?php echo json_encode($project); ?>;
However when I put my form on the server an error occurs and it points to this in the console.
var pageDownloadInfo=<br />
and the console gives me this notice error:
Notice: Undefined variable: project in C:\xampp\htdocs\QPA Form\resources\views\pages\datatest.blade.php on line 278
null;
The forms are exactly the same. I am not sure what is causing this.
try using quotes around the echo statement as such
var pageDownloadInfo="<?php echo json_encode($project); ?>";
Related
I have a php array of products that I want to move to javascript but when I json_encode it errors out giving me
Uncaught SyntaxError: Unexpected token ;
The debugger is giving me
var phpPro=;
and the code reads
var phpPro=<?php echo json_encode($pro)?>;
I have also encoded the array in the php script and tried to assign to js variable from there.
php =
$jpro=json_encode($pro);
js =
var phpPro=<?php echo $jpro;?>;
This code was actually working and now that I'm trying to copy it to another site it is not working. All the products are in the php recordset when displayed so I know they are there.
I have a really simple autocomplete function attached to one of the fields on a CakePHP form. Here is the jQuery:
<script>
$(function() {
var availableTags = [<?=$suppliers?>];
$( "#MsrSupplier" ).autocomplete({
source: availableTags
});
});
</script>
And here is the code in my controller that pulls the dropdown values from the database.
//Dropdown suggestions for autocompleting the Suppliers field
$suppliers=$this->Msr->find('list',array('fields'=>'Msr.supplier'));
$strSuppliers='"'.implode('","',$suppliers).'"';
$this->set('suppliers',$strSuppliers);
Field code in my edit.ctp:
<?php echo $this->Form->input('supplier', array('type'=>'textbox', 'div'=>false,
'name'=>'supplier', 'id'=>'MsrSupplier')); ?>
This is working fine in my development environment, which is just a local installation of XAMPP running on Mac OS X. When I start typing a word into the field, I get autocomplete suggestions, complete with CSS formatting. When I push this out to my production server, however, I get the following error when I inspect the field:
Uncaught SyntaxError: Unexpected token <
The error points to this line in my jQuery code:
var availableTags = [<?=$suppliers?>];
So, I see the problem, but I don't know why it's a problem or what to do about it. The Apache version in XAMPP is 2.4.16. The version on my server is 2.2.3-92 running on CentOS 5. Both environments are running CakePHP version 2.0.5, and the default layouts, which point to my version of jQuery, are the same.
echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
http://php.net/manual/en/function.echo.php
Your server likely has the option disabled, hence it's being output as is, ie as <?=$suppliers?>, instead of being interpreted as PHP.
ps. generally I'd use json_encode when passing data from PHP to JS, ie, do not build comma separated, quoted strings in your controller, but pass the query/results to the view as is, and then just do:
var availableTags = <?php echo json_encode($suppliers) ?>; // or <?= when applicable
I'm building a web application in CodeIgniter and I'm using jQuery and AJAX. I created the whole app locally (using XAMPP) and everything worked fine. After I uploaded the app to my web hosting, one AJAX keeps failing. Here is the part of the code:
// Get all form inputs
var inputs = $('#app-options-existing-form :input[type="text"]');
// Put them in object as name=>value
var data = {};
for(i=0; i<inputs.length; i++) {
data[inputs[i]["name"]] = inputs[i]["value"];
}
// Put loader while AJAX is working
$(".app-content-container").html('<center><img class="loader" src="<?php echo base_url();?>/img/loader.gif" ></center>');
console.log(data);
// Generate POST request
$.post("<?php echo site_url("admin/ajax_app_options"); ?>",
{"add_existing_form_submited" : true, "data" : data2},
function (data) {
alert("test" + data);
});
Here's the console showing error and result of console.log(data)
First, I thought that the key ("d1d1d1") was the problem because I was first using "1-1-1" and after I manually changed it, it was working. But then I changed everything in "d1d1d1" and it doesn't work again. As I said, it works on XAMPP but not on server. Can be a problem in using full URL for AJAX, instead of relative one? But I'm using it in other AJAX requests as well and it works.
Pretty sure you problem is this guy '<center><img class="loader" src="<?php echo base_url();?>/img/loader.gif" ></center>'
Yours source is going to output literally to <?php echo base_url();?>/img/loader.gif which is of course not a real link. Therefore it is a resource that can not be loaded.
You might want to try instead using: '<center><img class="loader" src="/img/loader.gif" ></center>'
The base_url() function is just going to return '/' anyway.
Important! In general you can not write php in javascript. Or this would be a massive security hole that would give every user who visits your site unlimited access to your server.
I'm using CodeIgniter form helper, and this is what it does:
Uncaught SyntaxError: Unexpected token ILLEGAL
The code:
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"') ?>');
As you can see, I'm using a PHP inside of a JS, when I do <?= 'Test' ?> it works.
So it seems like its related with the CodeIgniter function.
As far as i know this error message can be caused by unknown/wrong characters in the code, and from what I saw in the firebug, this CI function is generating text with tabs and line breaks... and that is my problem I guess.
I may be wrong, so please correct me if so.
I will appreciate any solution for this problem.
Chances are you're screwing up your quotes and you need to change the way you're passing that last parameter to the dropdown.
$class = 'class="span3"';
jQuery('#window-1').append('<?= form_dropdown("hfdata", $hf_arr, set_value("hfdata"), $class) ?>');
To explain how PHP and Javascript works: Php is executed on the server, this give html output. You browser will get that output and can do anything with it. From this point javascript can do his job.
You are echoing the php function call to the users browser. Javascript can't call the php function. If you want to do this, then you need to make a php file call that returnes the result you want. But I guess there is an other problem. ( if you want to do this, escape the ' characters)
You need to execute the PHP code on the server. This will give a result. You send this result to the client. On the clients PC javascript will execute your javascript code.
If you want to generate with PHP the javascript code, then you can do something like this ( in PHP on the server!):
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata')); ?> ');
If this isn't what you want, then you are working on a design problem. Then it's a kind of dead end.
I found the answer myself... As I said, it was caused by the HTML output that was returned from the from_dropdown.
The solution is simple, remove all unwanted characters like line breaks:
<?php
$prepare = preg_replace('/^\s+|\n|\r|\s+$/m', '', form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"'));
?>
jQuery('#window-1').append('<?= $prepare ?>');
In my form, I want to do something with two fields:
"website_domain" and "website_ip_address"
I'm trying to use jQuery/JSON to call a PHP script, pass the website_domain to it, and receive JSON including the IP address of that website.
Problem/Symptoms Description:
It's partially working: On blur, it GETs the url of the PHP script. I can see that much in firebug. I get a 200 OK. Output from the PHP script is valid JSON according to JSONLint:
{"field":"website_ip_address","value":"74.125.225.70"}
But in Firebug, I don't have a JSON tab. I only have Params and Headers tabs. Not even a Response tab.
Needless to say, the website_ip_address field is also not being populated with the data I should be getting from the PHP script's JSON output.
My PHP Script:
It may be important to note that for now, this PHP script on a different domain from my application. Maybe my whole problem is cross-domain?
<?php
$domain = $_GET["domain_name"];
$ip = gethostbyname($domain);
// echo $ip;
$json = array(
'field' => 'website_ip_address',
'value' => $ip,
);
header('Content-Type: text/plain');
echo json_encode($json );
?>
My jQuery/JSON script:
Note this is written inside a Ruby On Rails application view.
:javascript
$("#website_domain").bind("blur", function(e){
$.getJSON("http://exampledomain.com/temp_getIP.php?domain_name=" +$("#website_domain").val(),
function(data){
$('#website_ip_address').val(data);
});
});
I really hope this isn't just a syntax error on my part. I've been writing/rewriting this for 2 days, based on answers I've found on StackOverflow, to no avail. I'm just missing something here.
You are currently attempting to output the JS object (that is formed from the parsed JSON response) to the field. You need to output a value from within it. So not:
$('#website_ip_address').val(data); //data is an object, not a string
but
$('#website_ip_address').val(data.someValue); //output a property of the object
With your code as it is, I would expect the field to be populated with the string representation of an object, which is [object Object]. You don't mention this, so I wonder whether a) your success function is even firing (check this - stick a console.log in it); b) your jQ selector is sound.
The problem can be with different domain. I had it like this before. Try in php to add header("Access-Control-Allow-Origin: *")
Put your jQuery code inside document ready, example: $(function(){ });
:javascript
$(function() {
$("#website_domain").bind("blur", function(e){
$.getJSON("http://exampledomain.com/temp_getIP.php?domain_name="+$("#website_domain").val(),
function(data){
$('#website_ip_address').val(data);
});
});
});
});
And you have a missing end })