How to check if HTML forms are empty or not with PHP -


i wondering if there way to check if html text inputs empty, , if so, execute php code, provide.

html

<input name="name" type="text" class="form-control form-control-lg" id="exampleinputname2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">  <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleinputemail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">  <button name="mysubmitbtn" type="submit" class="btn btn-default btn-md">subscribe</button> 

php

if(!isset(['name']) || !isset(['email']) == 0){         $msg_to_user = '<h1>fill out forms</h1>'; } 

i wondering if:

  1. my php code correct syntax, believe it's not
  2. is possible check if input empty, , if empty, execute php code (specifically '$msg_to_user')
  3. it possible tweak code when user clicks submit button, , fields both empty, have $msg_to_user code execute

thank in advance!

you should use isset check on array - $_post or $_get according html form method.

it should be:

<form method="post">     <input name="name" type="text" class="form-control form-control-lg" id="exampleinputname2" placeholder="your name" maxlength="36" value="">     <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleinputemail2" placeholder="your email" maxlength="36" value="">     <button name="mysubmitbtn" type="submit" class="btn btn-default btn-md">subscribe</button> </form> 

php:

if (!isset($_post['name']) || !isset($_post['email'])) {     $msg_to_user = '<h1>fill out forms</h1>'; } else {     // process results } 

i guess, have answered questions , b. question c - it's architecture. example, if want post form page (i mean index.php file has form action='index.php' or empty action), can way (just example approach, not best):

if ($_server['request_method'] == 'post') {     if (!isset($_post['name']) || !isset($_post['email']))     {         $msg_to_user = '<h1>fill out forms</h1>';     } else {         // in database         if ($dosomethingindatabasesuccess)             $msg_to_user = '<h1>succesffully saved!</h1>'; // really? h1? :)         else             $msg_to_user = '<h1>something went wrong!</h1>'; } else {     $msg_to_user = '';  }  <form method="post">     <input name="name" type="text" class="form-control form-control-lg" id="exampleinputname2" placeholder="your name" maxlength="36" value="">     <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleinputemail2" placeholder="your email" maxlength="36" value="">     <button name="mysubmitbtn" type="submit" class="btn btn-default btn-md">subscribe</button> </form> <p class="result"><?php echo($msg_to_user); ?></p> 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -