0

I have some PHP which outputs JavaScript. I need to set the id dynamically. The following code breaks on the 3rd line because of the use of the i variable. This I expect to be a simple problem with the quotation marks but I could be wrong.

$html .='$("#add").click(function (e) {';

    $html .= 'var i=0;';

    //Append a new row of code to the "#items" div
    $html .='$("#items").append(\'<div><input name=\"input[]\" type=\"text\" id=\"itin_form_'+i+'\" /><button class=\"delete\">Delete</button></div>\'); i++; });';

Can anyone see why this code fails?

3
  • Is there a specific reason you need to do it this way and not through JavaScript/jQuery directly ? Commented Jun 7, 2016 at 21:32
  • Yes, I'm having to return $html at the end of a function, I'd prefer to do it another way but looks like I'm going to be forced to do it like this. Commented Jun 7, 2016 at 23:02
  • @user1532669 I found the issue first. Anyway, glad that it got solved. Did you see my answer? Commented Jun 8, 2016 at 9:12

3 Answers 3

3

Does this work?

$html .= 'var i=0;';
$html .='$("#add").click(function (e) {';
 //Append a new row of code to the "#items" div
$html .='$("#items").append(\'<div><input name="input[]" type="text" id="itin_form_\'+i+\'" /><button class="delete">Delete</button></div>\'); i++; });';

When escaped to Javascript it should return:

var i=0;
$("#add").click(function (e) {
    $("#items").append('<div><input name="input[]" type="text" id="itin_form_'+i+'" /><button class="delete">Delete</button></div>'); 
    i++; 
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'll help you even further. There is no need for escaping the double quotes, since they are in a string that's being defined with single quotes.
Yes, I understood that already and updated accordingly buddy. :D Check. :D
2

The problem here is, whenever you click, the value of i gets initialized to 0. It is indeed a simple silly mistake. Your code gives you this result:

$("#add").click(function (e) {
    var i=0;
    $("#items").append('<div><input name="input[]" type="text" id="itin_form_'+i+'" /><button class="delete">Delete</button></div>'); 
    i++; 
});

But it supposed to have something like this:

var i = 0;
$("#add").click(function (e) {
    $("#items").append('<div><input name="input[]" type="text" id="itin_form_'+i+'" /><button class="delete">Delete</button></div>'); 
    i++; 
});

See the placement of var i = 0; above. So your code should be:

$html .= 'var i=0;';
$html .='$("#add").click(function (e) {';
 //Append a new row of code to the "#items" div
$html .='$("#items").append(\'<div><input name="input[]" type="text\\" id=\"itin_form_\'+i+\'" /><button class="delete">Delete</button></div>\'); i++; });';

You don't need to escape " in the above code. :)

Comments

0

There's many issues with your code to begin with:

Variable var i=0 is declared inside the function so it's always getting reset and never going to increase over 1.

Solution: you have to declare it only once at the page load, e.g. in a script tag in your <head> section or inside a jquery document ready call. I showed an example below.

Please get rid of all these escape characters by using PHP nowdoc:

Here's an example test.php tested and working I created myself using your javascript code as-is (I just removed all the escape characters to make it more readable):

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<div id="items"></div>
<input type="button" id="add" value="Add" />

<?php

$str =
<<<'EOT'
<script>
/* Start of Nowdoc
   you can paste javascript code in here as-is!
*/
var i=0; // notice how I declared this outside of add.click
$("#add").click(function (e) {
    $("#items").append('<div><input name="input[]" type="text" id="itin_form_'+i+'" /><button class="delete">Delete</button></div>');
    i++;
}
);
/* End of Nowdoc */
</script>
EOT;


echo $str;

?>

Output:

enter image description here

Maybe you're just forgetting to include the jquery library.

Add this to your <head> section:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

1 Comment

@PraveenKumar: For other people that will fall on that answer on day and find something useful for themselves :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.