Page 1 of 1

Running javascript in php foreach

Posted: Thu Oct 13, 2011 9:55 am
by adige
Hello

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:
http://img36.imageshack.us/img36/4830/slidemanager.jpg


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:

Code: Select all

			<script language="JavaScript"><!--
				function onChange() {
					var Current = 
					document.myform.link.selectedIndex;
					document.myform.currentText3.value =
					document.myform.link.options[Current].value;
				}
				
				//-->
			</script>						

			<select name="link" onChange="onChange(this.value);">
			<?php
			if($_POST['action'] == save ) {
				$selected = "selected";
				}
				else{
				$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 ).'">';
				the_title();
				echo '</option>';
				endwhile;

				?>
			</select>			

			<input name="currentText3" type="text" value="">
But when i add this after the opening code of foreach, i.e:

Code: Select all

<?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:

Code: Select all

 <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.

Re: Running javascript in php foreach

Posted: Sat Oct 22, 2011 6:28 pm
by KCAstroTech
Ok, this is rather confusing. Could you let us know what lines in your code you are referring to? Also in your example above:

Code: Select all

                                function onChange() {
                                        var Current = 
                                        document.myform.link.selectedIndex;
                                        document.myform.currentText3.value =
                                        document.myform.link.options[Current].value;
                                }
you appear to be referring to the same form "myform" and the same text box "currentText3".