Javascript Multiple Values to PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepaks85
    New Member
    • Aug 2006
    • 114

    Javascript Multiple Values to PHP

    Dear All,

    I have an html page. There is a button 'Add Client' which adds multiple names and email address in the select box. You can have a look here:

    Click here to view sample

    After clicking on Continue button, I need to pass all added multiple names and email address to a php page. How do I do this?

    Can anyone help me on this?

    Thanks

    Deepak
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you'll find the named elements (of the form) in the $_GET array. but actually you pass it to an asp script.

    Comment

    • deepaks85
      New Member
      • Aug 2006
      • 114

      #3
      Sorry, I forgot to insert the html script:

      Code:
      <HTML><HEAD><TITLE>TRO</TITLE>
      <SCRIPT language=javascript>
      
      function textLimit(field, maxlen) {
          if (field.value.length > maxlen + 1)
              alert('You have exceeded the ' + maxlen + ' character maximum. Please revise your entry.');
          if (field.value.length > maxlen)
              field.value = field.value.substring(0, maxlen);
      } 
      </SCRIPT>
      <LINK href="http://www.travelresearchonline.com/favicon.ico" 
      rel="shortcut icon"><LINK href="TRO_files/interiorCSS.css" rel=Stylesheet>
      <META http-equiv=Content-Type content="text/html; charset=utf-8">
      <STYLE type=text/css>.style2 {
      	FONT-SIZE: 12px; FONT-FAMILY: Tahoma
      }
      .style3 {
      	COLOR: #ffffff; FONT-FAMILY: Tahoma
      }
      .style4 {
      	FONT-FAMILY: Tahoma
      }
      .style5 {
      	FONT-SIZE: 12px
      }
      .style6 {
      	FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: Tahoma
      }
      </STYLE>
      
      <META content="Microsoft FrontPage 5.0" name=GENERATOR></HEAD>
      <BODY>
      <TABLE height="100%" cellSpacing=0 cellPadding=0 width=765 align=center 
      bgColor=#ffffff border=0>
        <TBODY>
        <TR>
          <TD vAlign=top align=middle width=758>
            <DIV align=center>
            <SCRIPT language=javascript>
                              function addclient() {
                                  if(document.selectusers.clients.options.length < 5) {
                                      if(document.selectusers.clients.options[0].value == '0') {
                                          document.selectusers.clients.options[0] = null;
                                      }
                                      var newOp = new Option(document.selectusers.clientname.value + ": " + document.selectusers.clientemail.value, document.selectusers.clientname.value + "||" + document.selectusers.clientemail.value);
                                      document.selectusers.clients.options[document.selectusers.clients.options.length] = newOp;
                                      document.selectusers.clientname.value = "";
                                      document.selectusers.clientemail.value = "";
                                      document.selectusers.clientname.focus();
                                  } else {
                                      alert("Only five recipients are allowed per email.")
                                  }
                              }
                              function removeuser() {
                                  document.selectusers.clients.options[document.selectusers.clients.selectedIndex] = null;
                              }
                              function submitform() {
                                  if(document.selectusers.clients.options[0].value != '0' && document.selectusers.clients.options.length != 0) {
                                      for(i = 0; i < document.selectusers.clients.length; i++) {
                                          document.selectusers.clientshidden.value += document.selectusers.clients.options[i].value + "[sp]";
                                      }
                                      return true;
                                  } else {
                                      alert("Error: You must click the \“add client\” button to add email addresses before proceeding to the next step.")
                                      return false;
                                  }
                              }
                              </SCRIPT>
      
            <FORM name=selectusers onSubmit="return submitform()" action=step3.aspx method=get>
            <INPUT type=hidden name=clientshidden> 
            <P align=left> <BR><SPAN class=normalText>
            <TABLE width="100%" border=0>
              <TBODY>
              <TR>
                <TD vAlign=top align=left>
                  <TABLE border=0>
                    <TBODY>
                    <TR>
                      <TD class=normalText colSpan=2><I>Fill out name and email then 
                        click the "add client" button.</I></TD></TR>
                    <TR>
                      <TD class=normalText vAlign=top>Emails: </TD>
                      <TD style="FONT-SIZE: 9pt; FONT-FAMILY: Tahoma">
                        <TABLE border=0>
                          <TBODY>
                          <TR>
                            <TD align=left>Client Name: </TD>
                            <TD><INPUT size=30 name=clientname></TD></TR>
                          <TR>
                            <TD align=left>Client Email: </TD>
                            <TD><INPUT size=30 name=clientemail><INPUT onclick=addclient() type=button value="add client"></TD></TR>
                          <TR>
                            <TD></TD>
                            <TD align=left><SELECT style="WIDTH: 80mm" multiple 
                              size=5 name=clients><OPTION value=0>Add client names 
                                and emails</OPTION></SELECT> <BR><INPUT onclick=removeuser() type=button value="Remove Selected"></TD></TR></TBODY></TABLE></TD></TR>
                    <TR>
                      <TD class=normalText vAlign=top>Message: </TD>
                      <TD><TEXTAREA onKeyUp="textLimit(this, 400);" name=message rows=4 cols=43></TEXTAREA></TD></TR></TBODY></TABLE></TD>
                </TR></TBODY></TABLE></SPAN></P><INPUT type=hidden 
            value=bird_nature.jpg name=src> <INPUT type=hidden value=118 name=img> <INPUT type=submit value="Continue ->"> </FORM>
            </DIV></TD></TR>
        </TBODY></TABLE><!-- OPENTRACKER HTML START -->
      <SCRIPT src="TRO_files/server1_opentracker_net.htm" defer></SCRIPT>
      <NOSCRIPT>
      <A href="http://www.opentracker.net/" target=_blank><IMG 
      alt="web statistics" src="TRO_files/img_opentracker_net.gif" border=0></A></NOSCRIPT><!-- OPENTRACKER HTML END --></BODY></HTML>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        some advices in general:
        - put attribute values in quotation marks
        - identify javascript not with the language attribute but with its mime type: type="text/javascript"
        - you still get your form data to an asp page (to get it to php, insert a php file in <form>'s action attribute

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          To send multiple options from a <select> box into a PHP script, the name of the <select> box must end with [].
          Then, instead of just sending a single value, all selected options will be passed into the $_POST (or $_GET, if you prefer that) super-global as an array.

          And the <form> action attribute must of course point to a PHP script.
          Yours appears to be pointed at a ASP.Net page.

          Consider this:
          [code=php]
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <title>Multi-select test</title>
          </head>
          <body>
          <form action="?" method="post">
          <select name="MultiSele ct[]" multiple="multi ple" size="5">
          <option value="1">First </option>
          <option value="2">Secon d</option>
          <option value="3">Third </option>
          <option value="4">Fourt h</option>
          <option value="5">Fifth </option>
          </select><br>
          <input type="submit">
          </form>

          <pre><?php
          if(isset($_POST['MultiSelect'])) {
          echo "<b>Selecte d last time:</b> \n";
          foreach($_POST['MultiSelect'] as $_i => $_v)
          {
          echo "$_v\n";
          }
          }
          else {
          echo "<b>Nothing selected last time</b>";
          }
          ?></pre>
          </body>
          </html>
          [/code]
          This would print all selected options when you submit the page.

          On a different topic, note the <!DOCTYPE> at the top of that code.
          Omitting this may cause browsers to go into quirks-mode, which can make your page render incorrectly. A doctype should always be included at the top of your page so the browser knows which standard to use.

          Also, there are some errors in your HTML that you may want to fix.
          I recommend that you run it through the W3C validation tool.
          And even tho HTML4 technically doesn't require your attributes to be quoted, it's just good practice to do it anyways. Especially when the attribute value is JavaScript or something like that.

          Comment

          • deepaks85
            New Member
            • Aug 2006
            • 114

            #6
            Dear Atli,

            Thanks very much for your valuable time and reply

            Deepak

            Comment

            Working...