-1

I'm new to JQuery but for some reason I cant get the php code to display correctly when I put it in my JQuery script. The php code will display correctly when its not in the JQuery code can someone help me fix my JQuery code so it will display my PHP code correctly?

Here is the JQuery and PHP code.

var count = 0;
$(function(){
    $('p#add_field').click(function(){
        count += 1;
        $('#container').append(
            '<input type="text" name="sk" id="sk" />'
            + '<label for="exp">Exp: </label>'
            + '<?php'
            + 'echo \'<select id="exp" name="exp">\' . "\n";'
              + 'foreach($options as $option) {'
                + 'if ($option == $exp) {'
                  + 'echo \'<option value="\' . stripslashes(htmlentities(strip_tags($option))) . \'" selected="selected">\' .' 
                  + ' stripslashes(htmlentities(strip_tags($option))) . \'</option>\' . "\n";'
                + '} else {'
                  + 'echo \'<option value="\'. stripslashes(htmlentities(strip_tags($option))) . \'">\' .' 
                  + 'stripslashes(htmlentities(strip_tags($option))) . \'</option>\'."\n";'
                + '}'
              + '}'
            + 'echo \'</select>\';'
            + '?>'
            + '<label for="g">RGB: </label>'
            + '<?php'
            + 'echo \'<select id="g" name="g">\' . "\n";'
              + 'foreach($options as $option) {'
                + 'if ($option == $g) {'
                  + 'echo \'<option value="\' . stripslashes(htmlentities(strip_tags($option))) . \'" selected="selected">\' .' 
                  + 'stripslashes(htmlentities(strip_tags($option))) . \'</option>\' . "\n";'
                + '} else {'
                  + 'echo \'<option value="\'. stripslashes(htmlentities(strip_tags($option))) . \'">\' .' 
                  + 'stripslashes(htmlentities(strip_tags($option))) . \'</option>\'."\n";'
                + '}'
              + '}'
            + 'echo \'</select>\';'
            + '?></li>' );

    });
});
1
  • 2
    You can't embed PHP in JavaScript and expect it to process the PHP. PHP is a server-side language. Commented Apr 20, 2010 at 5:38

2 Answers 2

1

PHP is a server side language, and jQuery runs on the client/browser, so you cannot add PHP code after the page is loaded. You'll need to use PHP to write the code to the page like you normally do, but you can hide it with CSS and use the jQuery to display this section of your page when you want to.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use directly load method to append html

$('#container').load('ajax/example.php');

see jquery load documentation, In example.php write your php and html code

Comments

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.