javascript - How to echo values from a PHP string into different iterations of a loop

问题描述:

I have creted a tool in WP admin that searches the custom post type ($apartments), returns a list of $apartments with their post meta, allows the user to check a checkbox next to selected posts and finally email those selected posts to the client. Its an $apartments recommendation tool.

Currently I use some JS to get a string of post ID's along with an email address from selected apartments, then send them to a function that queries the DB and returns those posts inside a HTML template before using wp_mail() to send them out.

I dont save these posts, everything is generated on the fly. The comment boxes (.comments) are used on the fly and I would like to also pass these to the query function however, if I add these to their own JS string as I have done the post ID's, how do I then echo these out to their appropriate items with my loop. The first and third item here have a comment, how would I add that comment to the first and third item within the loop.

I send all this data to the PHP function via ajax so this would be posted as a JS string to a PHP function.

<div class="search-reults">

<ul>

<li class="item <?php echo $ID; ?>">

<p>apartmentname</p>

<textarea class="comments">some comments</texarea>

<input type="checkbox" name="selectthis">

</li>

<li class="item <?php echo $ID; ?>">

<p>apartmentname</p>

<textarea class="comments"></texarea>

<input type="checkbox" name="selectthis">

</li>

<li class="item <?php echo $ID; ?>">

<p>apartmentname</p>

<textarea class="comments">some comments</texarea>

<input type="checkbox" name="selectthis">

</li>

<li class="item <?php echo $ID; ?>">

<p>apartmentname</p>

<textarea class="comments"></texarea>

<input type="checkbox" name="selectthis">

</li>

</ul>

</div>

jQuery('.item:has(input:checked)').each(function() {

var str = jQuery(this).find('input[type=checkbox]').val();

postidstring += postidstring.length > 0 ? ',' + str : str;

});

On click this send the postidstring via ajax to a wp_query() where it will return the apartments in a loop and send out the resulting HTML via wp_mail();

网友答案:

So you want to know how to automate the process ? right ?

First, you need to store the list of apartments to an array.
Assuming you know how to get the list of apartments from the DB via MySQL query, I'll skip to the next step.