Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.