-2

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>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
CoderTn
  • 985
  • 2
  • 22
  • 49

3 Answers3

3

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.

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
1

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>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
1

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

  • 1
    `Wouldn't it also be – IsThisJavascript Mar 26 '18 at 09:48
  • He is checking `$_POST` variables which is why I put that as I'm assuming it's posted data he wants. –  Mar 26 '18 at 09:59
  • haha sorry mate what? :D I'm answering your edit. It doesn't *have* to be a ` – IsThisJavascript Mar 26 '18 at 10:00
  • sorry pal, misread :) isn't a button preferred due to basic browser functionality? e.g. Hit enter and form submits. I don't remember the last time I actually wrote `` https://stackoverflow.com/a/33663114/5283119 is a good explanation. –  Mar 26 '18 at 10:04
  • Yes I'd say it would be preferred but it's not a requirement and doesn't solve the question. I'd suggest editing your answer to make it more clear that a button would be better practice. – IsThisJavascript Mar 26 '18 at 10:13