简体   繁体   中英

isset condition not working in php

i was trying to print some text using echo , at first i thought that the error is from the syntax of echo function , then i figured out that the isset condition is not verified even if i set the button on !!

<?php
if (isset($_POST['ajout'])) {
    echo "string";
} else {
    echo "vérifier les champs";
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form>
            <label>id</label>
            <input type="text" name="id">
            <br>
            <label>points</label>
            <input type="text" name="points">
            <br>
            <input type="submit" name="ajout">
        </form>

    </body>
</html>

You should specify the form's method.

<form method="post">

According to w3c the default method is GET.

method = get|post

This attribute specifies which HTTP method will be used to submit the form data set. Possible (case-insensitive) values are "get" (the default) and "post". See the section on form submission for usage information.

try this one

you must use form method for get and post data into php

like <form method="post">

<?php
if (isset($_POST['ajout'])) {
    echo "string";
}
else{
    echo "vérifier les champs";
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>
        </title>
    </head>
    <body>
        <form method="post">
            <label>id</label>
            <input type="text" name="id">
            <br>
            <label>points</label>
            <input type="text" name="points">
            <br>
            <input type="submit" name="ajout">
        </form>
    </body>
</html>

The only $_POST variables here would be 'name && 'id', you are checking if a submit button has been set?

You are also missing your form method, which in this case should be "post"

<form method="post">

edit:

Using <button type="submit"> instead of <input type="submit"> is generally more accepted.

Although both elements deliver functionally the same result, I strongly recommend you use <button> :

  • Far more explicit and readable. input suggests that the control is editable , or can be edited by the user; button is far more explicit in terms of the purpose it serves
  • Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases
  • Predictable requests: IE has verying behaviours when values are submitted in the POST / GET request to the server
  • Markup-friendly; you can nest items, for example, icons, inside the button.
  • HTML5, forward-thinking; as developers, it is our responsibility to adopt to the new spec once it is officialized. HTML5, as of right now, has been official for over one year now, and has been shown in many cases to boost SEO .

In summary, I highly discourage use of <input type="submit" /> .

Source: Input Type Submit vs Button Type Submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM