Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Running javascript in php foreach

    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:

    PHP Code:
                <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:

    PHP Code:
    <?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 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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Why do you invoke the onchange function and pass the value of the <select>, thus:
    Code:
                <select name="link" onChange="onChange(this.value);">
    but then utterly ignore that passed value in the actual function:
    Code:
        function onChange() {
    As I read your code, you could have simply written
    Code:
                <select name="link" onChange="this.form.currentText3.value = this.value;">
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found a similar example that is need to be adapted for my case:

    Code:
     $(document).ready(function() {                                       
        $('.name').live('change', function() {
          $('.phonenumber').val($(this).find('option:selected').attr('data-phonenumber'));
        })
     });  
    
    <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>
    </select>
    
    <input type="text" class="phonenumber" name="phonenumber" value="" readonly="readonly">
    What i would like to have is sort of this which doesnt work:
    http://jsfiddle.net/adige72/BS9rp/

    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •