简体   繁体   中英

JQuery and PHP display problem

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>' );

    });
});

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM