www.webdeveloper.com
Results 1 to 6 of 6

Thread: if posting multiple php

  1. #1
    Join Date
    Apr 2012
    Posts
    15

    if posting multiple php

    i want to post to a different php depending on a drop down select. for example; option value "2" English to French, should post to "send_english.php", where as option value "3" French to English Should post to send_french.php. The closest i have got to this incorporates javascript, but any method you can think of would be good. Below is the javascript but it does not work.

    </script>
    <script type="text/javascript">
    function check() {
    var f = document.getElementById('form1');
    var s = document.getElementById('select1');
    if (s.selectedIndex == "2") {
    f.setAttribute("method", "POST");
    f.setAttribute("action", "send_english.php");
    f.submit("Translate");
    }
    if (s.selectedIndex == "3") {
    f.setAttribute("method", "POST");
    f.setAttribute("action", "send_french.php");
    f.submit("Translate");
    }
    }
    </script>
    </head>
    <body>
    <div style="height: 22px">Translate English to French</div>


    <form name="contactform" form id="form1">
    <table width="450px" class="style26">
    <tr>
    <td valign="top">
    <span class="style16">
    <label for="english">Type words in Selected Language</label></span>
    </td>
    <td valign="top">
    <input type="text" name="english" maxlength="50" size="30">
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align:center">
    <input type="submit" value="Translate">
    </td>
    </tr>
    </table>
    <select id="Select1" name="language" onclick=""
    <option value="1"></option>
    <option value="2">English to French</option>
    <option value="3">French to English</option>
    </select>
    </form>

  2. #2
    Join Date
    Feb 2011
    Posts
    231
    Hi,
    Try this code:
    Code:
    <script type="text/javascript">
    function check(val) {
    var f = document.getElementById('form1');
    if (val == 2) {
      f.setAttribute("action", "send_english.php");
      f.submit();
    }
    else if (val == 3) {
      f.setAttribute("action", "send_french.php");
      f.submit();
    }
    }
    </script>
    </head>
    <body>
    <div style="height: 22px">Translate English to French</div>
    
    
    <form name="contactform" id="form1" method="post">
    <table width="450px" class="style26">
    <tr>
    <td valign="top">
    <span class="style16">
    <label for="english">Type words in Selected Language</label></span>
    </td>
    <td valign="top">
    <input type="text" name="english" maxlength="50" size="30">
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align:center">
    <input type="submit" value="Translate">
    </td>
    </tr>
    </table>
    <select id="Select1" name="language" onchange="check(this.value)">
    <option value="1"></option>
    <option value="2">English to French</option>
    <option value="3">French to English</option>
    </select>
    </form>

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Don't forget to close your opening SELECT tag, and give your event something to do:
    Code:
    <select id="Select1" name="language" onclick="check();">
    <option value="1"></option>
    <option value="2">English to French</option>
    <option value="3">French to English</option>
    </select>
    Also, since you're using selectedIndex and not the VALUE of the selectedIndex, don't use quotes around 2 or 3. And it would be a bit simpler to use a switch/case statement:
    Code:
    function check() {
      var f = document.getElementById('form1');
      var s = document.getElementById('select1');
      switch(parseInt(s.selectedIndex)) {
        case 2:
          f.setAttribute("method","POST");
          f.setAttribute("action","send_english.php");
          f.submit(); // You don't need any parameters to submit a form
        break;
        case 3:
          f.setAttribute("method","POST");
          f.setAttribute("action","send_french.php");
          f.submit(); // You don't need any parameters to submit a form
        break;
        }
    }

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    And your FORM element needs a default action for those of us who do not use JavaScript. A submit button inside a NOSCRIPT element wold be nice too.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Apr 2012
    Posts
    15
    thanks. WolfShade's code didn't work, but MarPlo is getting there. But instead of it posting when selected, could it be posted when clicked on the submit (translate) button instead?

  6. #6
    Join Date
    Apr 2012
    Posts
    15
    nevermind. i fixed this by copying the onclick="check(this.value)" from the select to the submit, and changing it to onclick="check(language.value)".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles