conditional statements - isset condition not working in php

541

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>
398

Answer

Solution:

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.

895

Answer

Solution:

try this one

you must use form method forget andpost data intophp

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>
725

Answer

Solution:

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 whichinput[type="submit"] do not display correctly in some cases
  • Predictable requests: IE has verying behaviours when values are submitted in thePOST/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

People are also looking for solutions to the problem: php - Symfony bundle allow custom provider

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.