0

Im making a form that checks the Name input and echos results when the submit button is pressed using a IF statement. But i cant get it to work. What am i doing wrong?

<form id="AUTO"   method="post" action="" novalidate>
    <input type="hidden" name="token" value="<?php echo $token; ?>"/>
    <input type="hidden" name="miles" value=""/>
    <div id="contact_name">FULL NAME: *<br>
        <input id="element_2_1" name="name" class="element text" size="15" 
            maxlength="15" value="" type="text" placeholder="FULL NAME">
    </div>  
    <input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>

$Name = $_POST['Name'];

if (isset($_POST['submit'])) {
    //checks name field for a number if one exist echo has a number
    if (preg_match('|[0-9]', $Name)) {
        echo 'has a number';
    } else {
        echo 'Does not contain a number';
    }
}
Jonas Staudenmeir
  • 21,093
  • 4
  • 33
  • 77
  • First of all turn on error reporting while developing: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – ino Nov 30 '18 at 20:59
  • You can print `$_POST` variable with [`print_r`](http://php.net/manual/en/function.print-r.php) and see what you are receiving – Matteo Meil Nov 30 '18 at 21:08
  • error_log is in my cpanel does the same thing just found it – james gasrdner Nov 30 '18 at 21:40

3 Answers3

0

The form input tag is named name however you are trying to get value POST index of Name.

Correct would be

$Name = $_POST['name'] ;

Keep the case of names.

ino
  • 1,698
  • 1
  • 12
  • 21
  • He's gonna need to add something like if(isset($_POST['name']){ //code } to avoir some errors. The pattern is wrong too, something like /^[0-9]$/ should do the job – Dice Nov 30 '18 at 23:57
0

Forms are key are sensitive if you set the form name as lowercase

    <input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">

and in server side you should catch it as lowercase like this below.

 $name = $_POST['name'];

That's how you can call the variable from the form in your server side.

Hope this helps you to solve your problem.

Thanks

Jesus Erwin Suarez
  • 1,311
  • 14
  • 15
0
<form id="AUTO"   method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="Name" name="Name" class="element text" size="15" maxlength="15" type="text" placeholder="Full Name" value=""></div>   
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>




<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$Name = $_POST['Name'] ;




if (preg_match('#[0-9]#',$Name)){
echo 'has number';
}else{
    echo 'no number';

}





?>