How to access the form's 'name' variable from PHP

后端 未结 12 2032
小鲜肉
小鲜肉 2020-11-29 09:07

I\'m trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.

I realise that I could use hidden tags to solve my pr

相关标签:
12条回答
  • 2020-11-29 09:35

    As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.

    To prevent an XSS attack, where you have written:

    <?php echo "$weight"; ?>
    

    You should write instead:

    <?php echo htmlentities($weight); ?>
    

    Which could even be better written as:

    <?=htmlentities($weight); ?>
    
    0 讨论(0)
  • 2020-11-29 09:36

    In the form submitting button (id method of form is post):

    <input type="submit" value="save" name="commentData">
    

    In the PHP file:

    if (isset($_POST['commentData'])){
        // Code
    }
    
    0 讨论(0)
  • 2020-11-29 09:36

    Use a unique value on the submit button for each form like so

    index.html

    <form method="post" action="bat/email.php">
        <input type="text" name="firstName" placeholder="First name" required>
        <input type="text" name="lastName" placeholder="Last name" required>
        <button name="submit" type="submit" value="contact">Send Message</button>
    </form>
    
    <form method="post" action="bat/email.php">
        <input type="text" name="firstName" placeholder="First name" required>
        <input type="text" name="lastName" placeholder="Last name" required>
        <button name="submit" type="submit" value="support">Send Message</button>
    </form>
    

    email.php

    <?php
        if (isset($_POST["submit"])) {
          switch ($_POST["submit"]) {
              case "contact":
                  break;
              case "support":
                  break;
              default:
                  break;
          }
        }
    ?>
    
    0 讨论(0)
  • 2020-11-29 09:36

    I had a similar problem which brought me to this question. I reviewed all the preceding answers but ultimately I ending up figuring out my own solution. I hope it helps someone else with this same issue:

    <form name="ctc_form" id="ctc_form" action='' method='get'>
    
    <input type="hidden" name="form_nm" id="form_nm">
    
    <button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
    
    </form>
    

    It seamlessly and efficiently accomplishes the following:

    • Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
    • Works with both GET and POST methods.
    • Requires no additional, independent JavaScript.
    0 讨论(0)
  • 2020-11-29 09:38

    You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.

    0 讨论(0)
  • 2020-11-29 09:39

    Only the names of the form fields are submitted, the name of the form itself is not. But you can set a hidden field with the name in it.

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