Jump to content

PHP form what I can add fields with a button.


bambinou1980

Recommended Posts

Hello,

 

I am still on the learning curve with PHP and mysql, I can create normal cruds but this time I need a CRUD where I can add fields and set a dropdown menu.

 

Example:

 

I have a products table with prices

 

 

I want the admin to be able to pass an order and click  on an "add product" button and have a dropdown menu with the data of the products showing each time, and perhaps have this done 10 times or more...

 

Any idea how to do this please?

 

Thank you.

 

Ben

Link to comment
Share on other sites

Do you have any code written for this idea? Can you show us what you have?

 

If you wanted to do this strictly with php/html alone, you would most likely want to use the $_SESSION system or store the selected products in a database table so that you know what they have selected. Every time they click the Add Product button you could have a form that appears and adds another product to the $_SESSION data. There is no real limit on how many products you could add using that idea.

 

The basic workflow of this idea would be something like:

  1. Press Add Product button which is actually a link to the Add Product page
  2. On the Add Product page you display the dropdown selection and a Submit button in a <form> when that gets clicked you add whatever data was in that form to something like $_SESSION['products'] and then redirect them back to the main page showing all their currently selected products.
  3. Back on the currently selected products page, you foreach ($_SESSION['products'] as $products) and echo out whatever information is in there into a table of some type.

The other option would be to look into javascript which would allow you not require visiting other pages and redirecting users around. 

  • Like 1
Link to comment
Share on other sites

Thanks iarp, wow, I will have to check this out, All I have at the moment is this:

					   <form role="form" method="post" action="index.php">
					
						<div class="form-group">
							<label for="name_id">
								Customer Name *
							</label>
							<input type="text" name="customer_name" placeholder="Customer Name" class="form-control" id="name_id" required/>
						</div>
						
						<div class="form-group">
							<label for="due_date_id">
								Due Date *
							</label>
							<input type="text" name="due_date" placeholder="" class="form-control" id="duedate_id" required/>
						</div>
						
						
						


						
						
						
						
						<!--Product 1-->
						<?php 
						$query2 = "SELECT name, price1, price2, price3 FROM products ORDER BY name desc";
                        $result2 = mysqli_query($connection, $query2) or die (mysqli_error());
                        while($row = mysqli_fetch_array($result2)){
						?>

						 <div class="form-inline well">
						 <label for="product1_id">Choose Product 1 *</label>
						<div class="form-group">
							   <select name="product1" id="product1"  class="form-control">
								   <option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option>
								</select>

						  
					     <label class="btn btn-default">
                        <input type="radio" id="price1_id" name="action" value="<?php echo $row['price1'];?>" /> €<?php echo $row['price1'];?>
                        </label> 
						
						<?php if(($row['price2']) != null) {
						echo "<label class='btn btn-default'>
                        <input type='radio' id='price2_id' name='action' value='{$row['price2']}'/> € {$row['price2']}
                        </label>";
						} ?>
						
						
						
						<?php if(($row['price3']) != null) {
						echo "<label class='btn btn-default'>
                        <input type='radio' id='price3_id' name='action' value='{$row['price3']}'/> € {$row['price3']}
                        </label>";
						} ?>
						

						<label for="product1_id">Qty</label>
						<input name="quantity1" id="quantity1" type="text" class="form-control" maxlength="4" size="4"><!--disabled="disabled"-->

						  </div>
						  </div>
                         <!--Product 1-->
						<?php } ?>
           

						
						
						<div class="form-group">
							<label for="address_id">
								Address *
							</label>
							<textarea type="text" name="address" placeholder="Company's Address" rows="4" cols="50" class="form-control" id="address_id" value=""  required></textarea>
						</div>
						
						<div class="form-group">
							<label for="phone_id">
								Phone
							</label>
							<input type="text" name="phone" placeholder="Customer Phone" class="form-control" id="phone_id" value=""  />
						</div>
						
						<div class="form-group">
							<label for="email_id">
								Email
							</label>
							<input type="email" name="email" placeholder="Customer Email" class="form-control" id="email_id" value=""  />
						</div>
						
						<div class="form-group">
							<label for="vat_id">
								V.A.T Number(ie:MT20343324)
							</label>
							<input type="text" name="vat" placeholder="V.A.T Number"  class="form-control" id="vat_id" value=""  />
						</div>
						<button type="submit" name="submit" class="btn btn-default">
							Creat Order
						</button>
						</div> 

					</form>

o this be done in Jqeury?

 

I have a mistake in my code by the way, at the moment the while loop is adding the same number of horizontal dropdown menu than ids for the product which is wrong, this is why having a "add" field button would have been much better.

Link to comment
Share on other sites

making a select/option menu that has only one choice, repeated for each product, makes no sense. what is the purpose of the select/option menu in your code?

 

you would typically display all available products at once, ordered by category and/or name, or if you have a large number of products, provide a search box, category selection menu/links, or use pagination to limit what's being displayed at one time.

 

the one-time non-product information for the order would normally be entered as a separate step, not as part of the product selection.

 


 

you should not store the three prices in separate columns in your products table. multiple prices should be stored in another table, one row for each price, tied back to the products table using the product id. you can then store any number of prices for any product. when you JOIN the two tables to display the information, you will only get a row in the result set where there is a price. your code to produce the (three) radio buttons would simply loop over however many rows the JOINed query returns and produce that number or radio buttons. any time you find yourself repeating code, that only differs in the value it uses (such as your 3 radio button logic), it's a sign that you should be using a loop of some kind rather than writing out the code n number of times for each possible input value.

 


 

before you move up to using jquery/ajax, you need to be able to produce the client side and server side code that accomplishes  your task, since you will still need all of that client and server code when using jquery/ajax. jquery/ajax isn't required to make certain types of pages work. they only allow you to dynamically do things in the client without refreshing the page. without using them only means that when you perform an action in the client, the resulting output completely comes from the server.

Edited by mac_gyver
Link to comment
Share on other sites

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.