【问题标题】:html form not grabbing correct variables from surveyhtml表单没有从调查中获取正确的变量
【发布时间】:2022-01-16 16:36:43
【问题描述】:

我正在构建一个调查表,它使用 HTML(也可以使用 javascript)收集学生的信息,并且在网上找到了几个示例,但似乎没有一个有效。调查表目前只选择这些:

调查应该选择所有选项(名字、姓氏、居住地、大学选择、专业、校园访问),我不知道如何解决这个问题。

这是调查和代码:

 <h3> Senior Survey </h3>
    <div class="w3-container">

      <script type="text/javascript">
        console.log("Prospective student form is about to be displayed.");
      </script>

      <form method="post" action="mailto:hernand_gabr@bentley.edu" method="GET" enctype="text/plain" name="seniorsurvey">
        <table class="w3-table w3-blue" id="second-gradient">
          
          <tr>
            <td style="text-align: center";> 
            <p><b> Please enter your first and last names. </b></p>
            <input type="text" id="firstname" maxlength="60" placeholder="First Name:"/> 
            <input type="text" id="lastname" maxlength="60" placeholder="Last Name:"/>  
            </td>
          </tr>

          <tr>
            <td style="text-align: center";> 
              <p><b> Where do you live? </b></p>
              <input type="radio" id="peru" name="radiobutton" value="Peru">
              <label for="peru">Peru</label>

              <input type="radio" id="southamerica" name="radiobutton" value="SA">
              <label for="southamerica">South America</label>

              <input type="radio" id="unitedstates" name="radiobutton" value="USA">
              <label for="unitedstates">United States</label> 
            </td>
          </tr>

          <tr>
            <td>
              <fieldset style="text-align: center";>
                <p><b> What were your other top college choices? </b></p>
                <input type="checkbox" id="bentley" name="uni">
                <label for="bentley">Bentley U.</label>

                <input type="checkbox" id="brandeis" name="uni">
                <label for="brandeis">Brandeis U.</label>
                
                <input type="checkbox" id="boston" name="uni">
                <label for="boston">Boston U.</label>
                
                <input type="checkbox" id="babson" name="uni">
                <label for="babson">Babson U.</label>     
                
                <input type="checkbox" id="northeastern" name="uni">
                <label for="northeastern">Northeastern U.</label>               
              </fieldset>
            </td>
          </tr>

          <tr>
            <td style="text-align: center";> 
              <p><b> What is your prospective major? </b></p>
              <select name="majors" id="majors">
                <option value="un">undecided</option>
                <option value="cis">Computer Information Systems</option>
                <option value="fi">Finance</option>
                <option value="acc">Accounting</option>
                <option value="ci">Creative Industries</option>
                <option value="mkt">Marketing</option>
              </select>
              <br>
            </td>
          </tr>

          <tr>
            <td style="text-align: center";> 
              <p><b> When do you plan to visit campus? </b></p>
              <input type="date" id="start" name="visit-start" value="2021-10-25" min="2021-10-25" max="2030-12-31">
              <br><br>
            </td>
          </tr>

          <tr>
            <td style="text-align: center";> 
              <button type="submit" value="Submit College Info"> Submit </button>
              <button type="reset" value="Clear College Info"> Clear  </button>
              <br><br>
            </td>
          </tr>

        </table>
      </form>      

    </div>

【问题讨论】:

    标签: javascript html forms post survey


    【解决方案1】:

    您的一些表单字段,例如

    <input type="text" id="firstname" maxlength="60" placeholder="First Name:"/> 
    

    没有name 参数,这是使数据正确发送到服务器的粘合剂。您需要将该行更改为:

    <input type="text" name="firstname" id="firstname" maxlength="60" placeholder="First Name:"/> 
    

    基本上只是在其中添加 name="firstname"。您必须再次执行相同的操作,为id="lastname"-input 添加 name="lastname"


    这 3 个 &lt;input type="radio" 行都使用 name="radiobutton" ;从技术上讲,这没有错,但是考虑到上下文,您可能希望将它们中的所有 3 个更改为 name="country" 或类似的东西。这 3 个应该是相同的(因为人们只能选择这 3 个国家中的 1 个,而不是其中的 2 个或 3 个,也不能选择 0 个)。


    5 个&lt;input type="checkbox" 行都使用name="uni",这可能是错误的(虽然我实际上不能肯定地说)。人们可以选择它们的任意组合。您可能希望将它们更改为 5 个不同的名称(例如第一个名称为 name="uni_bentley",...),或者您可能希望保持它们相同。或许两者都试一下,看看你更喜欢哪两个结果(提交时选择几个/所有 uni)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多