SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    going directly with another input box

    Code:
    <script type='text/javascript'>
    window.onload = function()
    {
      document.getElementById('goDirect').onchange = ddmOnChange;
    }
    function ddmOnChange()
    {
      var url = this.options[this.selectedIndex].value;
    
      document.location = url;
    }
    </script>
    
    <form>
    <select id='goDirect'>
    <option>select</option>
    <option value='pg1.php'>Page 1</option>
    <option value='pg2.php'>Page 2</option>
    </select>
    </form>
    With the code above, if a user selects Page1, it will go directly to pg1.php without any clicking a submit button.

    I like to make it goes to pg1.php with another input text data without any clicking a submit button.

    The would-be code below doesn't work correctly, but I hope it show what I want.

    Code:
    <script type='text/javascript'>
    window.onload = function()
    {
      document.getElementById('goDirect').onchange = ddmOnChange;
    }
    function ddmOnChange()
    {
      var url = this.options[this.selectedIndex].value;
    
      document.location = url;
    }
    </script>
    
    <form method="post">
    
    <input type="text" name="myText" value="myText">
    <select id='goDirect'>
    <option>select</option>
    <option value='pg1.php'>Page 1</option>
    <option value='pg2.php'>Page 2</option>
    </select>
    </form>
    In pg1.php I have the code below and I like to get my target result below.
    Code:
    code in pg1.php
    <?php
    echo $_POST['myText'];
    ?>
    
    target result of pg1.php
    
    myText

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    I like to make it goes to pg1.php with another input text data without any clicking a submit button.

    The would-be code below doesn't work correctly, but I hope it show what I want.
    Yup, seeing what you want to do here. If you want to post data from a form, you'll have to submit the form to a page that will accept its data, for example pg1.php.

    One way to do it would be to change the "action" attribute of the form on the fly and then calling the submit method.
    e.g.:
    Code JavaScript:
    //set the form action
    var myForm = document.getElementById("theForm"); //assuming you added the id="theForm" to your form
    myForm.action = url;
    //and submit the form. This will make sure that form field data will get passed along
    myForm.submit();
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AussieJohn View Post
    Yup, seeing what you want to do here. If you want to post data from a form, you'll have to submit the form to a page that will accept its data, for example pg1.php.

    One way to do it would be to change the "action" attribute of the form on the fly and then calling the submit method.
    e.g.:
    Code JavaScript:
     
     
     
    //set the form action
    var myForm = document.getElementById("theForm"); //assuming you added the id="theForm" to your form
    myForm.action = url;
    //and submit the form. This will make sure that form field data will get passed along
    myForm.submit();
    Should I put your code in function ddmOnChange() like the below?
    Code:
    <script type='text/javascript'>
    window.onload = function()
    {
      document.getElementById('goDirect').onchange = ddmOnChange;
    }
    function ddmOnChange()
    {
      var url = this.options[this.selectedIndex].value;
    
      document.location = url;
    
    var myForm = document.getElementById("theForm"); //assuming you added the id="theForm" to your form
    myForm.action = url;
    //and submit the form. This will make sure that form field data will get passed along
    myForm.submit();
    
    }
    </script>
    
    
    <form method="post">
    
    <input type="text" name="theForm" value="myText">
    <select id='goDirect'>
    <option>select</option>
    <option value='pg1.php'>Page 1</option>
    <option value='pg2.php'>Page 2</option>
    </select>
    
    </form>
    However, the code above seems not to pass the form data to pg1.php or pg2.php.

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You need to make sure that the form has the id of theForm.

    Additionally, the document.location line will need to be removed, it will be redirecting users before the form has a chance to be submitted.

    Once you've done that, it should work
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

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