Loop PHP forms a chosen number of time from drop box - php

<div id="form1" />
<form name="choice">
<table>
<tr>
<td>
Number of new types you want to add
</td>
<td>
<select name="<?php echo $num; ?>">
<option value="1" selected>1 row</option>
<option value="2">2 rows</option>
<option value="3">3 rows</option>
<option value="4">4 rows</option>
<option value="5">5 rows</option>
</select>
<div class='inputs'></div>
</td>
</tr>
</table>
</form>
<form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);">
<table border="1">
<tr>
<th>Barcode<em>*</em></th>
<th>Description<em>*</em></th>
<th>Minimum required stock<em>*</em></th>
<th>Current stock</th>
</tr>
<?php
echo $num;
?>
<tr>
<td><input type="text" name="bar_code/></td>
<div class="ui-widget">
<td><input name="description/></td>
</div>
<td><input type="text" name="minimum_required_stock" /></td>
<td><input type="text" name="num_stock" value="0"></td>
<tr>
<td> </td>
<td> <button data-theme="b" id="submit" type="submit">Add accessories</button></td>
</tr>
</table>
</form>
Hello guys,
In this code I have 2 form, 1 is the drop box has 5 options and a form for text input.
In theory, if I choose option value="3", the text form will be loop 3 times and get 3 set of text inputs. However, I haven't succeeded in doing this by far. And also, how can I access these 3 sets of data in addAccessories.php file?
I appreciate any opinion on this topic.
Thank you very much.

Reading your question, it sounds like you want to have code such that when the selection in form choice changes, you change the number of rows in form myform. This is not possible from php because it will be a runtime change on the client machine. You can write this in javascript. You will need code called from form choice that adjusts the DOM in form myform.

You can either use a pure PHP solution and rebuild the page - but that's clunky and slow (which, might be fine for a pet project). Normally we'd use Javascript with AJAX to accomplish something like this.

Related

Adding a drop down list to a table, that is linked to a SQL database

I have an insert script and an update script, both separate. The script submits the data to the database, and adds or updates the database.
The scripts work within a table.
Part of the code:
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="750" border="0" cellspacing="1" cellpadding="5">
<tr>
<td width="100">Staff ID</td>
<td><input name="staff_id" type="text" id="staff_id">
<th>
Enter Desired ID
</th>
</td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
I want to add some options to the table, like a drop down menu.
How would I go about that?
Use the select tag, like so:
<select name="myselect">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Source: W3 Schools Select tag
The value selected will be available to your PHP script as $_POST['myselect'] or whatever name you choose for the select tag.
Also, as Devon mentioned in his comment, make sure your form is implemented properly and following PHP rules. If you're posting the form to the same page the form is on, you need to ensure its in a PHP file and not an HTML file. Also, change the "<?php $_PHP_SELF ?>" part to "<?php echo $_SERVER['PHP_SELF']; ?>" and ensure all the HTML in your file is either echoed or outside the <?php ?> tags

$_POST not getting initialized

I am using php for the first time. I understand that creating a HTML form initilizes variable like $_POST, $_GET, $_REQUEST etc.
I have the following code in my AddItem.php:
<?php require_once('../get-common/keys.php') ?>
<?php require_once('../get-common/eBaySession.php') ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>AddItem</TITLE>
</HEAD>
<BODY>
<FORM action="AddItem.php" method="post">
<TABLE cellpadding="2" border="0">
<TR>
<TD>listingType</TD>
<TD>
<select name="listingType">
<option value="Chinese">Chinese</option>
<option value="Dutch">Dutch</option>
<option value="FixedPriceItem">Fixed Price Item</option>
<option value="StoresFixedPrice">Stores Fixed Price</option>
</select>
</TD>
</TR>
<TR>
<TD>primaryCategory</TD>
<TD>
<select name="primaryCategory">
<option value="14111">Test Category</option>
<option value="57889">Boys Athletic Pants</option>
<option value="57890">Boys Corduroys Pants</option>
<option value="57891">Boys Jeans Pants</option>
<option value="57892">Boys Khakis Pants</option>
</select>
</TD>
</TR>
<TR>
<TD>itemTitle</TD>
<TD><INPUT type="text" name="itemTitle" value="TEST IN SANDBOX BEFORE PROD - DO NOT BID" size=30></TD>
</TR>
<TR>
<TD>itemDescription</TD>
<TD><INPUT type="text" name="itemDescription" value="TEST IN SANDBOX BEFORE PROD - DO NOT BID - This will incur prod listing fees" size=30></TD>
</TR>
<TR>
<TD>listingDuration</TD>
<TD>
<select name="listingDuration">
<option value="Days_1">1 day</option>
<option value="Days_3">3 days</option>
<option value="Days_5">5 days</option>
<option value="Days_7">7 days</option>
</select>
(defaults to GTC = 30 days for Store)
</TD>
</TR>
<TR>
<TD>startPrice</TD>
<TD><INPUT type="text" name="startPrice" value="<?php echo rand(1,200) / 100 ?>"></TD>
</TR>
<TR>
<TD>buyItNowPrice</TD>
<TD><INPUT type="text" name="buyItNowPrice" value="<?php echo rand(299,599) / 100; ?>"> (set to 0.0 for Store)</TD>
</TR>
<TR>
<TD>quantity</TD>
<TD><INPUT type="text" name="quantity" value="1"> (must be 1 for Chinese)</TD>
</TR>
<TR>
<TD colspan="2" align="right"><INPUT type="submit" name="submit" value="AddItem"></TD>
</TR>
</TABLE>
</FORM>
<?php
if(isset($_POST['listingType']))
//....some more code
?>
On running the file using php -f AddItem.php only the HTML gets printed. Nothing inside the <?php ?> gets executed. This is because $_POST is not initialized (prints Array on echo).
Could someone please point out what I'm doing wrong here. Why is the $_POST not getting initialized?
Thanks
There is no HTTP call in php command line execution in your case, its pretty normal that $_POST will be empty.
As a work around, i would suggest doing something like the following:
Write a script that would run under a web server like Apache.
Include or Require your PHP script.
The following code snippet will clarify my claims:
<?php
require_once('/path/to/your_command_line_php_script.php');
// Now inside your_command_line_php_script.php you will have $_POST available and initialized.
Hope this helps.
If you run php in the way you do. You are executing the script. But not actually posting the form. You will have to submit the form through a browser to populate the $_POST vars

Form submitting to wrong url in php

I am creating a simple registration form in html and submitting it via post method to register.php which is under a directory named controller. When I submit the form, it redirects to wrong URL.
This is the form:
<form action="controller/register.php" method="post" enctype="multipart/form-data" id="pre-registration-form">
<fieldset>
<legend><span>Type of Entry</span></legend>
<table id="rally-type">
<tr>
<td>Sponsored</td>
<td>Non-sponsored</td>
</tr>
<tr>
<td><input type="radio" id="xtreme" name="entry-type" value="xtreme" /></td>
<td><input type="radio" id="ndure" name="entry-type" value="ndure" /></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>
<span>Vehicle Details</span>
</legend>
<table>
<tr>
<td>Category</td>
<td><select name="vehicle-category" id="vehicle-category-list">
<option value="0">Select</option>
<option value="2whlr">2 Wheeler</option>
<option value="2whlr">4 Wheeler</option>
<option value="2whlr">4 Wheel Drive</option>
</select></td>
<td>Make</td>
<td>
<select name="vehicle-make">
<option value="0">....</option>
<option value="1">....</option>
.
.
.
</select>
</td>
</tr>
<tr>
<td>Model</td>
<td><input type="text" name="make-model" /></td>
<td>Year</td>
<td><input type="text" name="make-year" /></td>
</tr>
</table>
</fieldset>
.
.
.
.
<input type="submit" name="submit" value="Submit" />
When I try to submit the form, it redirects to controller/index.php rather than controller/register.php
When I rename register.php file to index.php then the firefox gives the following error:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This
problem can sometimes be caused by disabling or refusing to accept
cookies.
I have tried this on wamp as well as lamp server and both produces the same error. What might be the root of this problem?
It looks like in controller/register.php u have any check which redirect it to index of controller. please make it sure otherwise it will not redirect it to any page so make sure of this.
Try
<form action="/controller/register.php" method="post" enctype="multipart/form-data" id="pre-registration-form">
Or
<form action="../controller/register.php" method="post" enctype="multipart/form-data" id="pre-registration-form">

Add another text box when a user select other option

My form is looking like this.
<form action="add_customers.php" method="post" onsubmit="MM_validateForm('name','','R','passport_no','','R','remarks','','R');return document.MM_returnValue">
<tr>
<td>Name of the Applicant:</td>
<td><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Country applying for :</td>
<td><label for="country"></label>
<select name="country" id="country">
<option>Singapore</option>
<option>Malaysia</option>
<option>Istanbul</option>
<option>Thailand</option>
<option>China</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Visa Categeory :</td>
<td><label for="visa_categeory"></label>
<select name="visa_categeory" id="visa_categeory">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select></td>
</tr>
<tr>
<td>Passport No:</td>
<td><input type="text" name="passport_no" id="passport_no" /></td>
</tr>
<tr>
<td>Remarks:</td>
<td><label for="remarks"></label>
<textarea name="remarks" id="remarks" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr></form>
Now My problem is
1) when a user select other option from Visa Category then automatically display a text box for user and capture that what he enter.
2) save that details into mysql database
Used languages are PHP,MYSQL
Thanks.
You can add a text box in your form and make it hidden first time.
Get the textbox visible on change of the dropdown you have mentioned using javascript.
You can get the value of the textbox in the page where you accepts the data and insert into data database in add_customers.php along with other variables.
<script>
function visacatOnchange(){
var visa_cat = document.getElementById('visa_categeory').value
if(visa_cat == "Visit")
document.getElementById("new_textbox").style.display="block";
else
document.getElementById("new_textbox").style.display="none";
}
</script>
In html form add
<input type="text" name="new_textbox" id="new_textbox" style="display:none;">
and change
<select name="visa_categeory" id="visa_categeory" onChange="visacatOnchange();">
Good luck :-)
Initially you can keep the textbox div hidden and then display it on "onchange" event of select input.
<div id="textboxdiv" style="visibility: hidden">
Visa Option <input type="text" id="someid"/>
</div>
<select name="visa_categeory" id="visa_categeory" onchange="showTextBox()" >
<option>Visit</option>
<option>Business</option>
<option value="99" >Other</option>
</select>
Using Jquery you can display it like this:-
function showTextBox(){
if ($('#visa_categeory').val() == '99') {
$('#textboxdiv').css({'visibility':'visible'});
}
Here's some basic JavaScript/jQuery that should help you solve your first problem: http://jsfiddle.net/j4aXQ/
HTML
<form>
<select name="visa_category">
<option>Visit</option>
<option>Business</option>
<option>Other</option>
</select>
</form>
JS
$('select').change(function() {
var $selected = $('select option:selected');
if ('Other' === $selected.text()) {
$('form').append($('<input>').prop({'type':'text','id':'visa_other','name':'visa_other'}));
$('form').append($('<label>').prop({'for':'visa_other'}).html('Testing'));
} else {
$('input').remove();
$('label').remove();
}
});
The above code will only display a text box (and accompanying label) if the "Other" option is selected, and will then remove that text box if a different option is subsequently picked.
Hope that helps!

Issue Posting data

I am trying to post the name="pricetag" and name="size" which is in the item and price... I know this is not working because I have use echo but nothing is coming out but if I echo the pid of the product I shows. The codes below
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td name="pricetag" id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td name="size" id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this, this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
click here for live code http://jsfiddle.net/J4rXX/5/ by Ghillied
It doesn't POST because forms don't submit contents from TD elements. Only from INPUTs, SELECTs and TEXTAREAs.
More information on HTML FORMs here. If you want to avoid displaying the same data twice, you should try INPUT type=hidden.
<table>
<tr>
<td>Pricetag</td><td>PRICE_HERE</td>
</tr>
</table>
<form>
<input type="hidden" name="pricetag" value="PRICE_HERE" />
...
</form>
This should do nothing to hurt your existing JS scripts.
The only information that is posted to the server from a HTML form are elements such as INPUT, SELECT, and TEXTAREA.
If you want to post additional data, but make it un-editable to the user, you need to ideally put the data into INPUT elements which have the attributes type="hidden". This will post the data from the field but make them hidden to the user posting the form.
Adding the elements anywhere in the form will work. I've positioned them at the bottom of the form element, with the already existing hidden field, in this snippet.
I've also removed the name attributes from the TD elements in the snippet, to conform with web standards.
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this,this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pricetag" value="£25.00" id="hiddenpricetag" />
<input type="hidden" name="size" value="12 inch" id="hiddensize" />
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
EDIT:
Just noticed that the form uses javascript, upon the change of the SELECT data, to change the TD elements HTML data.
To make sure that the form posted the correct data that is in the TD elements, you would need to add ID tags to the hidden input fields so that you can program the javascript to update these as well as the TD elements.
I've added the IDs hiddenpricetag and hiddensize to the two hidden fields.
Your javascript function should be somewhat like this:
function addCharge(select, elemIndex)
{
var item = select.getElementsByTagName('option')[elemIndex];
var price = document.getElementById('pricetag');
var sizeDOM = document.getElementById('item');
var hiddenpricetag = document.getElementById('hiddenpricetag');
var hiddensize = document.getElementById('hiddensize');
var finalPrice = "";
/* Getting the price and showing it up */
finalPrice = parseFloat(item.text.substring(11, item.text.length-1));
/* We set a default £25.00 value */
if(isNaN(finalPrice)) finalPrice = "25";
/* we add decimal */
price.innerHTML="£"+finalPrice+".00";
/* Getting the size and showing it up */
var size = item.text.substring(0,7);
sizeDOM.innerHTML = size;
/* Altering the hidden field values */
hiddenpricetag.setAttribute("value", "£"+finalPrice+".00");
hiddensize.setAttribute("value", size);
}
JSFiddle can be found here: http://jsfiddle.net/J4rXX/7/

Categories