Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP isset not working with empty

Tags:

javascript

php

I'm building a form so users can add their own products to sell on a website. The javascript should check if all fields are filled out, though it isn't working. Why isn't the javascript running?

The PHP is a little more complicated. This website sells products in different packages to allow for wholesale and retail pricing, so multiple pricing options are available for different numbers of units. However, if one of the optional pricing options or unit numbers are filled in, but the corresponding value isn't, I think it will cause problems down the line, and there is no reason for this to occur other than a user typo.

To avoid this, if price2 is set, and units2 is empty, I want the form to be rejected and "You need to fill out both units and prices for each package option" to be echoed. The same is true for the inverse and price3 and units3.

So that's what the PHP is supposed to do. However, the first "if" triggers every time, so "You need to fill out both units and prices for each package option1" echoes. Why is this not running correctly?

<script>
function validproform()
{
var a=document.forms["productentry"]["pname"].value;
var b=document.forms["productentry"]["pword"].value;
var c=document.forms["productentry"]["about"].value;
var d=document.forms["productentry"]["unitabout"].value;
var e=document.forms["productentry"]["price1"].value;
var f=document.forms["productentry"]["units1"].value;
if (a==null || a=="" || b==null || b=="" || c==null || c=="" || d==null || d=="" || e==null || e=="" || f==null || f=="")
  {
  alert("Required fields are not all filled out.");
  return false;
  }
</script>

<?php
if (isset($_POST['psubmit'])) {
    if(isset($_POST['price2']) and empty($_POST['units2'])){
        echo('You need to fill out both units and prices for each package option1');
    }

    elseif(isset($_POST['units2']) and empty($_POST['price2'])){
        echo('You need to fill out both units and prices for each package option2');
    }

    elseif(isset($_POST['price3']) and empty($_POST['units3'])){
        echo('You need to fill out both units and prices for each package option3');
    }

    elseif(isset($_POST['units3']) and empty($_POST['price3'])) {
        echo('You need to fill out both units and prices for each package option4');
    }
}
?>

<form name="productentry" action="" method="post" onsubmit="return validproform()">
Product Name: <input type="text" name="pname" maxlength="30"><br>
About: <input type="text" name="about" maxlength="250"><br>
Describe your product. What is unique about it, what does it do, what sets it apart?<br>
What each unit contains: <input type="text" name="unitabout" maxlength="250"><br>
For example: Are you selling, "A bag of chips" or "A box w/ 30 bags of chips" or "a carton of 10 boxes with 30 bags of chips each." Describe what one unit contains.<br>
Price: <input type="text" name="price1" maxlength="30"><br>
Number of units: <input type="text" name="units1" maxlength="30"><br>
----
Price 2(optional): <input type="text" name="price2" maxlength="30"><br>
Number of units(optional): <input type="text" name="units2" maxlength="30"><br>
Price 3(optional): <input type="text" name="price3" maxlength="30"><br>
Number of units(optional): <input type="text" name="units3" maxlength="30"><br>

Password: <input type="password" name="pword" maxlength="30"><br>
<input type="submit" name="psubmit">
</form>
like image 537
EveyPortman Avatar asked Jun 11 '26 03:06

EveyPortman


1 Answers

   Try this code:

     <?php
     if (isset($_POST['psubmit']))
        {
         if(!$_POST['price2'])
              {
           echo('You need to fill out both units and prices for each package option1');
              }
       elseif(!$_POST['units2'])
              {
             echo('You need to fill out both units and prices for each package option2');
              }
        elseif(!$_POST['price3'])
              {
            echo('You need to fill out both units and prices for each package option3');
              }

        elseif(!$_POST['price3']) 
              {
            echo('You need to fill out both units and prices for each package option4');
             }
         }
     ?>

     The !$_POST will the return if the value is FALSE.
like image 182
Dante Ditan Avatar answered Jun 13 '26 19:06

Dante Ditan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!