Jump to content

PHP output from HTML form


freakunleash

Recommended Posts

Hi All,

 

Need help with PHP output from HTML form. I don’t know if I’m doing it correctly or not.

 

I have a form which ask to input data and output values in different page. It has multiple input field for same value and its value is stored in an array. If I submit the form PHP is calculating the empty fields also. I don’t want it to display values if there is no input.

 

Below is the PHP and HTML code of one field.

 

PHP Code

 

<?php

if(isset($_POST['product']))
{
   $product = $_POST['product'];
   $n        = count($product);
   $i        = 0;

   echo "Your selected Products are \r\n" .
        "<ol>";
   while ($i < $n)
   {
      echo "<li>{$product[$i]}</li> \r\n";
      $i++;
   }
   echo "</ol>";
}
?>

 

 

HTML FORM

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product details</title>
<style type="text/css">
body {
background-color: #C9F;
}
</style>
</head>

<body>
<b>
<h1>Create new Product entry:</h1>

<form id="form1" name="form1" method="post" action="submit1.php">
  <p>Product:<br />
    <label for="product"></label>
    <input name="product[]" type="text" id="product" size="15" />
    <input name="product[]" type="text" id="product" size="15" />
    <input name="product[]" type="text" id="product" size="15" /><br />
    <input name="product[]" type="text" id="product" size="15" />
    <input name="product[]" type="text" id="product" size="15" />
    <input name="product[]" type="text" id="product" size="15" />
    
    <br />
  
<input name="submit" type="submit" id="send" value="submit">
</form>

</b>
</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/226875-php-output-from-html-form/
Share on other sites

<?php

if(isset($_POST['product']))
{
   $product = $_POST['product'];
   $n        = count($product);
   $i        = 0;

   echo "Your selected Products are \r\n" .
        "<ol>";
   while ($i < $n)
   {
if($product[$i] == ""){
	//do nothing
	$i++;
}else{
      echo "<li>{$product[$i]}</li> \r\n";
      $i++;
  }
   }
   echo "</ol>";
}
?>

 

Added:

if($product[$i] == ""){
	//do nothing
	$i++;
}else{
      echo "<li>{$product[$i]}</li> \r\n";
      $i++;
  }

 

Denno

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.