-1

I want to know what can be the way using php and html such that I am provided a dropdown of books, I need to select exactly two choices out of that drop down and then calculate sum of price of both the books.

Assuming I had hardcoded the Books say:

Book 1 - $5
Book 2 - $15
Book 3 - $50

I know with if it was to select only one book. But no idea for this one. Please help

Code :

<?php
if(isset($_POST['formSubmit'])) 
{
    $varCurrentBook = $_POST['formBook'];
    $errorMessage = "";

    if(empty($varCurrentBook)) 
    {
        $errorMessage = "<li>You forgot to select a Book!</li>";
    }

    if($errorMessage != "") 
    {
        echo("<p>There was an error with your form:</p>\n");
        echo("<ul>" . $errorMessage . "</ul>\n");
    } 
    else 
    {
        switch($varCurrentBook)
        {
            //Can use here to find what option is clicked
        }
        exit();
    }
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <label for='formBook'>Select a Book</label><br>
    <select name="formBook">
        <option value="0">Select a Book...</option>
        <option value="15">The Secret</option>
        <option value="10">The Fairy Tales</option>
        <option value="5">All about words</option>
        <option value="100">Pinaacle Studio</option>
        <option value="120">Harry Potter</option>
        <option value="200">Thinking in Java</option>
   </select> 
   <input type="submit" name="formSubmit" value="Submit" />
</form>
ms8
  • 417
  • 2
  • 13
  • 1
    The calculation is done client side or server side? Maybe do it with JS? – chris85 Jul 07 '15 at 18:56
  • @chris85 But I need to do it with PHP only. Assume that calculation is done on server side – ms8 Jul 07 '15 at 18:58
  • @devz I had added code part I had done – ms8 Jul 07 '15 at 19:02
  • you can't select multiple books. that's a single select right now. – Marc B Jul 07 '15 at 19:03
  • @MarcB Thats what am looking for. How can I do it. If I make it multiple selective than how to maintain the sum ? – ms8 Jul 07 '15 at 19:05
  • use jQuery Ajax each time a selection is made send a request to server then update the page with new amount use checkboxes BTW the for attribute for label must have the id value of the input element. – AAB Jul 07 '15 at 19:06
  • @AAB I need to use dropdown only. And other than that you can help to provide code ? – ms8 Jul 07 '15 at 19:08
  • Do you use Jquery for your application – AAB Jul 07 '15 at 19:15

3 Answers3

0

You need to use a multiple select as:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for='formBook'>Select a Book</label><br>
<select name="formBook[]" multiple><!--The multiple attribute-->
    <option value="0">Select a Book...</option>
    <option value="15">The Secret</option>
    <option value="10">The Fairy Tales</option>
    <option value="5">All about words</option>
    <option value="100">Pinaacle Studio</option>
    <option value="120">Harry Potter</option>
    <option value="200">Thinking in Java</option>
</select> 
<input type="submit" name="formSubmit" value="Submit" />
</form>

Also note how I changed name from formBook to formBook[]. This changes $_POST['formBook'] to an array of options.

Then you can access it as:

<?php
  foreach ($_POST['formBook'] as $names)
  {
    print "You have selected $names<br/>";
    /*Your also need to change your code here accordingly.*/
  }

?>
Raman
  • 2,735
  • 1
  • 26
  • 46
0

As ARBY answered correct you have to add the multiple attribute to your select element:

<select name="formBook" multiple>

This sends the value attribute. (E.g. 0 / 15 / 10)

So you can check if you have exactly two books like this:

//Check if the user has selected exactly two books
$formBooks = $_POST['formBook'];
if(count($formBooks) !== 2){
    //TODO: return error message that user has to select two books
}

//Calculate the value
$result = array_sum($formBooks);

//TODO: return value
devz
  • 2,629
  • 2
  • 31
  • 37
0

You have to use multiple select with jquery see: HTML Multiselect Limit

<select name="formBook"> 
have to become:
<select name="formBook[]" multiple">

Then you can sum all selected elements

if(isset($_POST['formSubmit'])) 
{
    $sum = array_sum($_POST['formBook']); // Sum of all selected elements
    ...
}
Community
  • 1
  • 1
cNb
  • 67
  • 1
  • 9