HTML form submit to PHP script

前端 未结 6 1253
刺人心
刺人心 2020-12-10 11:51

I am making an HTML form. I want the results to appear in the PHP script.


提交评论

  • 2020-12-10 12:26

    Here is what I find works

    1. Set a form name
    2. Use a default select option, for example...

      <option value="-1" selected>Please Select</option>

    So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.

    1. Try to use HTML5 attributes now they are supported.

    This input

    <input type="submit">
    

    should be

    <input name="Submit" type="submit" value="Submit">
    

    whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.

    You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

    0 讨论(0)
  • 2020-12-10 12:28

    It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].

    You will probably want the following HTML:

    <select name="website_string">
      <option value="" selected="selected"></option>
      <option value="abc">ABC</option>
      <option value="def">def</option>
      <option value="hij">hij</option>   
    </select>
    <input type="submit" />
    

    With some PHP that looks like this:

    <?php
    
    $website_string = $_POST['website_string']; // or $_GET['website_string'];
    
    ?>
    
    0 讨论(0)
  • 2020-12-10 12:31

    For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:

    <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
    
    0 讨论(0)
  • 2020-12-10 12:34

    Try this:

    <form method="post" action="check.php">
        <select name="website_string">
            <option value="" selected="selected"></option>
            <option VALUE="abc"> ABC</option>
            <option VALUE="def"> def</option>
            <option VALUE="hij"> hij</option>
        </select>
        <input TYPE="submit" name="submit" />
    </form>
    

    Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.

    check.php

    <?php
        echo $_POST['website_string'];
    ?>
    

    Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.

    0 讨论(0)
  • 2020-12-10 12:34
    <form method="POST" action="chk_kw.php">
        <select name="website_string"> 
            <option selected="selected"></option>
            <option value="abc">abc</option>
            <option value="def">def</option>
            <option value="hij">hij</option>   
        </select>
        <input type="submit">
    </form>
    


    • As your form gets more complex, you can a quick check at top of your php script using print_r($_POST);, it'll show what's being submitted an the respective element name.
    • To get the submitted value of the element in question do:

      $website_string = $_POST['website_string'];

    0 讨论(0)
  • 提交回复
    热议问题