简体   繁体   中英

embed PHP in jQuery .append()

Is this possible? I know the code below looks a whole heap of mess but i want to make it more messy by embedding PHP into it. On each click i'm appending a row onto a table, but i need to include a dynamic dropdown into one of these <td> 's by pulling results from a mysql db. Where it says this: <td><p class="add_edit">Add/Edit</p><input type="text" class="project_ref_input" name="project_ref_input" /><p class="project_ref"></p></td> Instead of p tags i'm going to have a PHP built dropdown...how can i achieve this?

$('#items').append('<tr class="tableRow">
<td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a></td>
<td class="supp_short_code">' + supp_short_code_db + '</td>
<td class="om_part_no">' + omPartNo + '</td>
<td>' + supPartNo + '</td><td>' + cat + '</td>
<td class="description">' + desc + '</td>
<td>' + manuf + '</td>
<td>' + list + '</td>
<td>' + disc + '</td>
<td><p class="add_edit">Add/Edit</p><input type="text" class="quantity_input" name="quantity_input" /></td>
<td class="price_each_nett price">' + priceEach + '</td>
<td class="cost_of_items"></td>
<td><p class="add_edit">Add/Edit</p><input type="text" class="project_ref_input" name="project_ref_input" /><p class="project_ref"></p></td>
<td class="cost_total_td"></td>
</tr>');

Because Jquery is client side - you cant append PHP like you suggest.

You would have to write a PHP script that is triggered by a callback from Jquery, the PHP script would recieve some parameters, and return the HTML that would be needed to achieve your solution.

Does this help?

step 1: add row

// Your code
//just call another function to get db driven combo.
get_education_combo();

step 2: write following javascript function for retriving the result from php code and sending to html element.

function get_education_combo()
{
    var url ="print_education_list";
    //alert(url);
    var contents = AjaxRequest(url);
    //alert(contents);
    //return contents;
    //send the result to html

    document.getElementById("elementID").innerHTML=contents;
}

function AjaxRequest(url)
{
    //alert(url);
    if(xmlhttp != null){
        if(xmlhttp.abort)
            xmlhttp.abort();
        xmlhttp = null;
    };
    if(window.XMLHttpRequest) // good browsers
        xmlhttp=new XMLHttpRequest();
    else if(window.ActiveXObject) // IE
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    if(xmlhttp == null)
        return null;

    xmlhttp.open("GET",url,false);

    xmlhttp.send(null);
    // alert(xmlhttp.status);

    if(xmlhttp.status >= 200 && xmlhttp.status < 300)// 2xx is good enough
        return xmlhttp.responseText;
    else
        return null;
}

step 3: php code

print_education_list()
{
    $education="your query";
    echo '<select name="edu_name" id="edu_name" style="width:70px;">';
    foreach($education as $edu)
    {
        echo '<option>';
        echo $edu->sEducationName;
        echo '</option>';
    }
    echo '</select>';
}

That's It. BEST OF LUCK. I have prepared this combination during development of DeskTop application by php.

You would generate the dropdown on the server, then fetch the dropdown using the jQuery $.ajax method. Alternatively you could return a JSON array of option/values and build your dropdown using something like $.each to iterate the array.

If you are thinking about having the PHP in the javascript then sending that back to the server to be executed then DON'T. That's a WTF of the highest order. You didn't mean that right? (You might want to change the title of your question - becasue that's what it looks like).

edit: For all you guys saying client side PHP is impossible. Check this out!

http://thedailywtf.com/Articles/Client-side_PHP.aspx

PHP is server-side only, so you can't embed it into the JS that you send to the client's browser and expect it to run. In order to achieve your result, you'll either need to use PHP to render the list in the initial page, or use an AJAX call to pull the list from a service URI.

If you are generating this code dynamically on the server, ie you want to add the mysql results to the HTML markup before sending it to the client, you would do something like this:

<td><?php $VARIABLE_TO_ADD ?></td>

This is assuming you know how to pull the data out of the DB and create a variable with that data. PHP looks for the <?php ?> tags within an HTML document and parses whatever is between them.

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