Embed PHP in jQuery.append ()

Is it possible? I know that the code below looks like a whole bunch of mess, but I want to make it messier by embedding PHP in it. Each time I click, I add a row to the table, but I need to include a dynamic drop-down menu in one of these <td> , pulling the results from mysql db. Where he 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 will have a drop-down list of PHP ... 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>'); 
+4
source share
5 answers

Since Jquery is the client side, you cannot add PHP as you suggest.

You will need to write a PHP script called by a callback from JQuery, the PHP script will receive some parameters and return the HTML that will be needed to reach your solution.

Does it help?

+8
source

step 1: add a line

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

Step 2: write the following javascript function to extract the result from php code and send to the 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>'; } 

It. GOOD LUCK. I prepared this combination while developing DeskTop application on php.

+3
source

You must create a drop-down list on the server, and then select the drop-down list using the jQuery $ .ajax method. Alternatively, you can return an array of JSON parameters / values ​​and build a drop-down list using something like $ .each to iterate through the array.

If you think that PHP in javascript is sent back to the server for execution, then DO NOT. This is a higher order WTF. Didn't you mean that? (You might want to change the name of your question - because of how it looks).

edit: For all of you guys that the client side of PHP is not possible. Check this!

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

+1
source

PHP is server-only, so you cannot embed it in the JS that you send to the client browser and expect it to start. To achieve the result, you will need to use PHP to display the list on the start page or use an AJAX call to list the service from the service URI.

+1
source

If you generate this code dynamically on the server, i.e. want to add mysql results to HTML markup before sending it to the client, you will do something like this:

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

It is assumed that you know how to pull data from a database and create a variable with that data. PHP searches for <?php ?> Tags in an HTML document and parses everything in between.

0
source

Source: https://habr.com/ru/post/1334670/


All Articles