2

I'm trying to dynamically create a div to show error messages using jquery. I'm able to use append() to create the HTML easily enough, but I need to call a php variable in order to display the content. If I just try adding php opening and closing tags like I would ordinarily, the append method doesn't behave as expected and outputs a seemingly random section from the middle of the line. How can I overcome this problem?

Here's the jquery code as I have it:

$(document).ready(function() {
    var errors_php = "<?php  echo validation_errors('<li>','</li>'); ?>";
    $('#wrapper').append("<div id='errors'><ul>"+errors_php+"</ul></div>");
    $('#errors').slideDown('slow');
});

Note: the validation_errors() function is a codeigniter method. Incidentally, if I remove the errors_php variable from the append() it works as expected, displaying an empty div.

EDIT:

The generated code is:

<div id="errors"><ul>',''); ?&gt;</ul></div>
musoNic80
  • 3,678
  • 9
  • 40
  • 48
  • 1
    And what is being output to the screen? – wowo_999 Jul 08 '10 at 17:04
  • sorry @wowo_999 I don't follow. The browser is parsing the generated code above and displaying it as expected (with a little css!). – musoNic80 Jul 08 '10 at 17:07
  • Related: http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines – John Rasch Jul 08 '10 at 17:09
  • @John Rasch I just looked up that question and it is exactly what I'm trying to do. Unfortunately, I don't understand from the answers how they've solved it. I've never used json data before. Where should I put that piece of code?!?! – musoNic80 Jul 08 '10 at 17:22
  • What does the line `var errors_php = "',''); ?>";` render as when the page is loaded? I mean, if you go into View Source and look at that line, what do you see? – Luke Stevenson Jul 09 '10 at 05:42
  • @Lucanos, it renders what I've called "generated code" in original post. – musoNic80 Jul 09 '10 at 19:01
  • @musoNic80 - I think Lucanos is looking for the actual line in the source code (in browser, right click and select "view source"). What *exactly* is `validation_errors()` returning. This is different from what the javaScript returns. – thetaiko Jul 09 '10 at 20:06

3 Answers3

3

Make sure that the output from the PHP is properly escaped. If you have a quotation mark (") in the error code it will cause problems. Try wrapping validation_errors with addslashes:

$(document).ready(function() {
    var errors_php = "<?php echo addslashes(validation_errors('<li>','</li>')); ?>";
    $('#wrapper').append("<div id='errors'><ul>"+errors_php+"</ul></div>");
    $('#errors').slideDown('slow');
});
thetaiko
  • 7,816
  • 2
  • 33
  • 49
  • I like your thinking, but I just tried it and got the same output. There shouldn't be any quotation marks or anything like that in the errors. – musoNic80 Jul 08 '10 at 17:15
  • Can you post the code for `validation_errors` and what some example errors might be? – thetaiko Jul 08 '10 at 18:51
  • validation_errors() is a function that is part of the codeigniter framework. It outputs predefined error messages following form submission eg. required field is empty etc. The problem isn't in this function, it's working out how to get jquery to send the browser php. – musoNic80 Jul 09 '10 at 19:07
  • Outside of the javascript function, run the following: `','')); ?>` What does it print out? – thetaiko Jul 09 '10 at 19:23
3

As per your comment:

I just looked up that question and it is exactly what I'm trying to do. Unfortunately, I don't understand from the answers how they've solved it. I've never used json data before. Where should I put that piece of code?

JSON data is simply JavaScript Object Notation, so if you set a JavaScript a variable to a JSON value it will essentially recreate a copy of the object that was serialized into JSON in the first place.

var errors_php = "<?php  echo validation_errors('<li>','</li>'); ?>";

should be:

var errors_php = <?php echo json_encode(validation_errors('<li>','</li>')); ?>;
John Rasch
  • 62,489
  • 19
  • 106
  • 139
0

You can't run PHP code in javascript file, but you can run html&JS code in PHP file.

Javascript runs in the user's browser.

PHP runs on the server. It reads your .php files, runs PHP that it finds, sends HTML to the browser, and exits.

There is no "spooky action at a distance" that allows you to run PHP code from within the Javascript code.

To run html&JS code in PHP file, do something like below:

$('.addctr').append(
'<div class="row">' +
'<div class="form-group col-sm-5">' +
'<label class="control-label">Parameter</label>' +
'<select class="form-control" name="asset_search">' +
'<?php foreach ($dd["asset_search"] as $asset_search) { ?>' +
'<option <?= set_select("asset_search", $asset_search["text"]) ?>><?= $asset_search["value"] ?></option>' +
'<?php } ?>' +
'</select>');
Ellix4u
  • 566
  • 5
  • 9