View Full Version : Running javascript in php foreach

10-13-2011, 04:43 PM

Firstly, this is the full pastebin code of my admin page because i failed adding full code here correctly: http://pastebin.com/tQAaQjYC

and the screenshot of the slide manager page:

Well, I have a Slide Manager as an admin menu page in Wordpress. When i click "Add new slide" button, as you see in the screenshot above, it adds a new "slide field" in which that dropdown list shows all titles of posts. (Ignore other parts as they work as expected)

What i would like to do is, when i choose a post title, the text box which is next to dropdown to be populated automatically by the post's url adress. I want it to be worked for every "slide field" added via "Add new slide" button.

I actually made it work that way outside the foreach:

<script language="JavaScript"><!--
function onChange() {
var Current =
document.myform.currentText3.value =


<select name="link" onChange="onChange(this.value);">
if($_POST['action'] == save ) {
$selected = "selected";
$selected = "";
echo '<option value="">...... Choose One ...... </option>';
$args = array(
'post_type' => 'post',
'nopaging' => true

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<option '.$selected.' value="'.get_permalink( $post->ID ).'">';
echo '</option>';


<input name="currentText3" type="text" value="">

But when i add this after the opening code of foreach, i.e:

<?php foreach($nivoslides as $k => $slide) : ?>

Each slide field shows the dropdown and text box properly as you see in screenshot BUT none of them work.

I tried to add brackets but it didn't work either:

<select name="link[]" onChange="onChange(this.value);">

I guess it's the javascript code that should be edited or entirely changed but i have no idea how to do it.

I would be grateful for any help. Thanks in advance.

Old Pedant
10-13-2011, 08:29 PM
Why do you invoke the onchange function and pass the value of the <select>, thus:

<select name="link" onChange="onChange(this.value);">

but then utterly ignore that passed value in the actual function:

function onChange() {

As I read your code, you could have simply written

<select name="link" onChange="this.form.currentText3.value = this.value;">

10-14-2011, 10:11 AM
I tried the code you suggested before the foreach (line 109: http://pastebin.com/tQAaQjYC) and it worked as well.

But when i add it after the foreach, as you can see in the full code that i edited, it didn't work either.

May be some jQuery is needed in this situation but how?

Old Pedant
10-14-2011, 08:41 PM
Well, I don't use PHP, so your code is useless to me.

If you want JavaScript people to look at it, do this:

Bring the page in question up in your browser.

Click on the VIEW menu.

Click on the SOURCE or PAGE SOURCE menu item.

Now you are seeing the HTML (including JavaScript) that the browser sees.

Copy/paste the relevant section of *THAT* code to here.

10-18-2011, 01:38 AM
I found a similar example that is need to be adapted for my case:

$(document).ready(function() {
$('.name').live('change', function() {

<select class="name" name="name">
<option value="" selected="selected">Please select...</option>
<option value="Elvis" data-phonenumber="11111">Elvis</option>
<option value="Frank" data-phonenumber="22222">Frank</option>
<option value="Jim" data-phonenumber="33333">Jim</option>

<input type="text" class="phonenumber" name="phonenumber" value="" readonly="readonly">

What i would like to have is sort of this which doesnt work:

If i select an option, all values of input text fields change but in fact i would like to only one text field to be populated which is next to that dropdown list i select from.

Is this possible?