Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Add jQuery count to PHP string ready for next time page loads

Hi all,

This is a combination of WordPress, PHP & jQuery.

What I'm doing is building a meta box for WordPress admin that has a series of tabbed options, each tab having a column of values. It's a menu thing so each input box is a menu item.

Now, I'm working on adding a button to add an additional row so that more items can be added. The button functions and everything but, the classes, names, ids and values all need to have a +1 count added to each new row.

In WordPress, to get the saved value to appear in the meta box once you've saved it, you need to use esc_attr($variable). $variable been the saved meta data.

Now I know that you can't change server data without using Ajax but, as there's a new request between when the info is entered and then requested, it doesn't matter. It doesn't need AJAX as you have hit save to save it.

My problem is, I need to add to the $variable a count. The same count as the new row so row 3 would be $variable_3.

Can I do this with jQuery or is there a way of doing it with jQuery and then shoehorning in the PHP tags?

Thanks.

3 Answers

This work?

value="<?php $counter = "<script>phpCounter</script>"; echo esc_attr($menu_item_ . $counter); ?>"

Put the previously declared in a PHP variable and then concatenate this to the variable that's added in the script.

So it echos out the string <script>phpCounter</script>, so kinda close!

If it doesn't matter what the specific extension of the $variable_ is, couldn't you just check the size of the existing array of tabs and add one?

Paul Graham - going down a different route. Putting the fields in an array. This way, each field will populate its own array...I think!