0

Hey guys I hope someone can help here, I imagine this is something small and simple but I can't figure it out

I have the following code:

if (isset($_POST['Flavours']) && isset($_POST['Sizes']) && isset($_POST['Product_Name'])){
		$flav = explode('-',$_POST['Flavours']);
		$flavours = $flav[0];
		$sizes = $_POST['Sizes'];
		$product_name = $_POST['Product_Name'];
		$result = mysql_query('SELECT Products_ID FROM products WHERE Size = "' . $sizes . '" AND Flavour = "' . $flavours . '" AND Product_Name = "' . $product_name . '"');
		$row = mysql_fetch_array($result);
		$product_id = $row['Products_ID'];
	}

What I want to do is validate the inputs on the php side. so ifflavours,prices and sizes are set do the following.

What I want to happen is that if the flavours or sizes is blank " ". then dont do anything.

I imagine it would be something like this but I can't get it to work:

if (isset($_POST['Flavours']) && isset($_POST['Sizes']) && isset($_POST['Product_Name']) || (($_POST['Sizes'] != ' ') || ($_POST['Flavours'] != ' ')))

Thanks for the help!

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by harshbavaliya
0

Don't combine logic like that into one giant `if`. Here's a quick example.

<?php
$fields = array('Flavours', 'Sizes', 'Product_Name');
$errors = array();
foreach ($fields as $field) {
	if (!isset($_POST[$field])) {
		$errors[] = 'Missing required field ' . $field;
		continue;
	}

	if (!strlen(trim($_POST[$field]))) {
		$errors[] = $field . ' cannot be empty.';
		continue;
	}
}

if (empty($errors))
{
	// I'm assuming you're cleaning these appropriately.... *hint hint wink wink*
	$flav = explode('-', $_POST['Flavours']);
	$flavours = $flav[0];
	$sizes = $_POST['Sizes'];
	$product_name = $_POST['Product_Name'];

	$result = mysql_query('SELECT Products_ID FROM products WHERE Size = "' . $sizes . '" AND Flavour = "' . $flavours . '" AND Product_Name = "' . $product_name . '"');
	$row = mysql_fetch_array($result);
	$product_id = $row['Products_ID'];
	$message = 'Blahity blah successful!';
}
else
{
	$message = 'YOU HAVE ERRORS! ' . join('<br />', $errors);
}
0
this is how you query empty fields.

(!$_POST['Flavours'] || strlen($_POST['Flavours'] = trim($_POST['Flavours'])) == 0)

if (isset($_POST['Flavours']) && isset($_POST['Sizes']) && isset($_POST['Product_Name']) || (($_POST['Sizes'] != ' ') 
||  (!$_POST['Flavours']|| strlen($_POST['Flavours'] = trim($_POST['Flavours'])) == 0) .............................Size and Product Name
0

I am using test1.htm as form... the code is as follows...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 
 </HEAD>

 <BODY>
 <form name="form" action="test1.php" method="post" onSubmit="return verify()">
<table width="200" border="0">
 <tr>
   <td>Flavours : </td>
   <td><input type="text" name="Flavours"><br></td>
 </tr>
 <tr>
   <td>Sizes : </td>
   <td><input type="text" name="Sizes"><br></td>
 </tr>
 <tr>
   <td>Product Name : </td>
   <td><input type="text" name="Product_Name"><br></td>
 </tr>
  <tr>
   <td><INPUT class="button" type="submit" name="submit" value="Submit"></td>
   <td> </td>
 </tr>
</table>

</form>

 </BODY>
</HTML>

and test1.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
<?php
if($_POST['Flavours'] != NULL && $_POST['Sizes'] != NULL && $_POST['Product_Name'] != NULL){
		$flav = explode('-',$_POST['Flavours']);
		$flavours = $flav[0];
		$sizes = $_POST['Sizes'];
		$product_name = $_POST['Product_Name'];
		echo "Everything is okey!";
 }
else
echo "Something is going wrong!";
?>



</BODY>
</HTML>

Just check it, it is working good on my machine....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.