Delete sql table data then insert multiple variable amount of rows - php

Could anyone help please?
trying to create a page for calendar entries for a band that they can update themselves on their own site.
html entry page:
<form action="addgigtimes.php" method="POST">
<fieldset class='multi'>
<legend><h5>Gig 1</h5></legend>
<label for='weekday' class='accessAid'></label>
<select id='weekday' name='weekday'>
<option value='' selected>Day of week</option>
<option value='Monday'>Monday</option>
<option value='Tuesday'>Tuesday</option>
<option value='Wednesday'>Wednesday</option>
<option value='Thursday'>Thursday</option>
<option value='Friday'>Friday</option>
<option value='Saturday'>Saturday</option>
<option value='Sunday'>Sunday</option></select>
<label for='day' class='accessAid'></label>
<select id='day' name='day'><option value='' selected>--Day--</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option></select>
<label for='month' class='accessAid'></label>
<select id='month' name='month'><option value='' selected>--Month--</option>
<option value='01'>01 (Jan)</option>
<option value='02'>02 (Feb)</option>
<option value='03'>03 (Mar)</option>
<option value='04'>04 (Apr)</option>
<option value='05'>05 (May)</option>
<option value='06'>06 (Jun)</option>
<option value='07'>07 (Jul)</option>
<option value='08'>08 (Aug)</option>
<option value='09'>09 (Sep)</option>
<option value='10'>10 (Oct)</option>
<option value='11'>11 (Nov)</option>
<option value='12'>12 (Dec)</option></select>
<label for='year' class='accessAid'></label><select id='year' name='year'>
<option value='' selected>--Year--</option>
<option value='2015'>2015</option>
<option value='2016'>2016</option>
</select>
venue
<input type="text" class="input" name="venue" />
location
<input type="text" class="input" name="location" />
time
<input type="text" class="input" name="time" />
</fieldset>
<button type="reset" style="border: 0; background: transparent">
<img src="images/reset.jpg" alt="reset" style="margin-right:19px "/>
</button>
<button type="submit" value="submit" style="border: 0; background:transparent">
<img src="images/submit.jpg" alt="submit" />
</button>
</form>
I will repeat the fieldset code 30 times so the band can enter a max of 30 gigs at once.
here is the php code:
$sql = "DELETE FROM gigtimes";
$weekday = $_POST['weekday'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$venue = $_POST['venue'];
$location = $_POST['location'];
$time = $_POST['time'];
$rt=mysql_query("insert into guestbook(weekday,day,month,year,venue,location,time) VALUES ('$weekday','$day','$month','$year','$venue','$location','$time')");
echo mysql_error();
So basically delete whats already on the database then update.
I'm just not sure how to go about the row ids? Could anyone explain.
Thanks,
Bettsy

I have a band like website in production, so I know your pain. One thing I do is insert the band name as well, so that way if the band wants to delete something, it searches for their name and deletes the field such as:
$sql = "DELETE FROM gigtimes WHERE band_name = '$band_name'";
It's not perfect, because in theory there could be multiple bands with the same name, so to prevent that, make band names unique. This works perfectly for me. Let me know if you need more information.

Related

PHP Script adding new records, but fields are all blank

I'm utilizing an html form and php to add data to a MySQL database. I've been able to establish a connection, and have the php script add a new record to the database, however, all fields are always empty. I've looked at solutions here and elsewhere, redone my script a few times, and still come up with the same issue.
Any help would be appreciated, php is new to me.
HTML
<label for="custFirstName">First Name</label>
<input type="text" id="custFirstName" placeholder="John">
<label for="custLastName">Last Name</label>
<input type="text" id="custLastName" placeholder="Doe">
<label for="custStreet">Street Address</label>
<input type="text" id="custStreet" placeholder="1234 Cedar Street">
<label for="custCity">City</label>
<input type="text" id="custCity" placeholder="Beverly Hills">
<label for="custState">State</label>
<select id="custState">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
<label for="custZipcode">Zip Code</label>
<input type="text" id="custZipcode" placeholder="90210">
<label for="custTX">Telephone Number</label>
<input type="text" id="custTX" placeholder="555-555-5555">
<label for="custEmail">Email Address</label>
<input type="email" id="custEmail" placeholder="name#example.com">
<button type="submit" name="submit">Submit</button>
</form>
PHP
<?php
$con = mysqli_connect('localhost','root','');
if(!con) {
echo 'Error: Not connected to the server.';
}
if(!mysqli_select_db($con,'dBaseName')) {
echo 'Error: Database is not selected';
}
if(isset($_POST['submit'])) {
$custFirstName = $_POST['custFirstName'];
$custLastName = $_POST['custLastName'];
$custStreet = $_POST['custStreet'];
$custCity = $_POST['custCity'];
$custState = $_POST['custState'];
$custZipcode = $_POST['custZipcode'];
$custTX = $_POST['custTX'];
$custEmail = $_POST['custEmail'];
$sql = "INSERT INTO Customer (custFirstName,custLastName,custStreet,custCity,custState,custZipcode,custTX,custEmail) VALUES ('$custFirstName','$custLastName','$custStreet','$custCity','$custState','$custZipcode','$custTX','$custEmail')";
if(!mysqli_query($con,$sql)) {
echo 'Error: There was a problem adding the record to the database.';
}
else {
echo 'Your information was successfully added to the database.';
}
}
header("refresh:2; url=http://www.success.html");
?>
You are using wrong tags, instead of id you need to use name. For example:
<input type="text" id="custZipcode" name="custZipcode" placeholder="90210">
You need to use "name" attribute in all your input and select tag, e.g:
<input type="text" name="custFirstName" id="custFirstName" placeholder="John">
Thanks

I want to pass the current "UserID" into the database in an Insert statement. php/mysql

I am currently working on a project where a user can review a product then when they "Submit", the UserID & PreferenceID are picked up from the DB & passed into another table in the DB using php/mysql.
The UserID is displayed when they are logged in.
Here is some of the code:
$user_id = $_GET['CustomerID'];
$DissCustPref_PrefID = $_GET['DissCustPref_PrefID'];
$Cigar_CigarID = $_GET['Cigar_CigarID'];
$SmokingTime = $_GET['SmokingTime'];
$CigarFlavour = $_GET['CigarFlavour'];
$FlavourStrength = $_GET['FlavourStrength'];
$Body = $_GET['Body'];
$AfterTaste = $_GET['AfterTaste'];
$Age = $_GET['Age'];
$ValueForMoney = $_GET['ValueForMoney'];
$OverallRating = $_GET['OverallRating'];
$user_id = $row->CustomerID;
print "<br>User ID: ";
echo($user_id);
$query = "INSERT INTO DissReview values
('null','.$user_id','.$DissCustPref_PrefID','$Cigar_CigarID',
'$SmokingTime','$CigarFlavour','$FlavourStrength','$Body',
'$AfterTaste','$Age','$ValueForMoney','$OverallRating')";
At the moment the URL looks like: http://www.somewebsite/foldername/Login/PreviousReviews.php?CustomerID=%24user_id&CustPref_PrefID=%24PrefID&CigarID=2&SmokingTime=2&CigarFlavour=6&FlavourStrength=5&Body=4&AfterTaste=2&Age=2&ValueForMoney=2&OverallRating=4
as you can see it is not getting the userID or the Preference ID.
It should look like CustomerID=1.... and so on.
I have tried using '".UserID."' but this does not work.
Any suggetions?
EDIT ------ Here is the form used to send the data:
Please add your review of the cigar here!
CIGAR
<textarea style="display:none;"></textarea>
<!--This is the rating for the amount of time it took to smoke.-->
<p> Units below are represented in minutes.</p>
<select name="SmokingTime">
<option value="0"> Please Select an Option</option>
<option value="1">15</option>
<option value="2">30</option>
<option value="3">45</option>
<option value="4">60</option>
<option value="5">61+</option>
</select>
<div id="FChart" class="FChart">
<p> <img src="cigar-flavors-wheel.png" alt="Cigar Flavour Wheel" style="width:500px;height:500px;"></p>
</div>
<!--This is the rating for how the cigar tastes.-->
<p> Please select what the cigar tastes like.</p>
<select name="CigarFlavour">
<option value="0"> Please Select an Option</option>
<option value="1">Flowers</option>
<option value="2">Plants</option>
<option value="3">Herbs and Spices</option>
<option value="4">Earths and Minerals</option>
<option value="5">Fruit</option>
<option value="6">Nuts</option>
<option value="7">Chocolate</option>
<option value="8">Coffee</option>
<option value="9">Cream</option>
<option value="10">Leather</option>
</select>
<!--This is the rating for the strength of the flavour.-->
<p> Please select how strong the taste is.</p>
<select name="FlavourStrength">
<option value="0"> Please Select an Option</option>
<option value="1">Very Weak</option>
<option value="2">Weak</option>
<option value="3">Mild</option>
<option value="4">Strong</option>
<option value="5">Very Strong</option>
</select>
<!--This is the rating for how the cigar is: Medium body for example.-->
<p> Please Select the body of the cigar.</p>
<select name="Body">
<option value="0"> Please Select an Option</option>
<option value="1">Mild</option>
<option value="2">Medium</option>
<option value="3">Medium Full</option>
<option value="4">Full</option>
</select>
<!--This is the rating for the strength of the after taste.-->
<p> Please Describe the Strength of the After taste.</p>
<select name="AfterTaste">
<option value="0"> Please Select an Option</option>
<option value="1">Very Weak</option>
<option value="2">Weak</option>
<option value="3">Mild</option>
<option value="4">Strong</option>
<option value="5">Very Strong</option>
</select>
<!--This is the rating for the age of the cigar.-->
<p> Please select a rough age of the cigar.</p>
<select name="Age">
<option value="0"> Please Select an Option</option>
<option value="1">6 Months</option>
<option value="2">1 Year</option>
<option value="3">2 Years</option>
<option value="4">3 Years</option>
<option value="5">4 years</option>
<option value="6">4+ years</option>
</select>
<!--This is the rating for the value for money.-->
<p> Please Describe the value for money aspect.</p>
<select name="ValueForMoney">
<option value="0"> Please Select an Option</option>
<option value="1">Very Poor</option>
<option value="2">Poor</option>
<option value="3">Reasonable</option>
<option value="4">Good</option>
<option value="5">Very Good</option>
</select>
<!--This is the overall rating of the cigar.-->
<p> Please select an overall rating of the cigar.</p>
<select name="OverallRating">
<option value="0"> Please Select an Option</option>
<option value="1">Very Poor</option>
<option value="2">Poor</option>
<option value="3">Reasonable</option>
<option value="4">Good</option>
<option value="5">Very Good</option>
</select>
">
I am echoing the $UserID as debugging to see if it is picking it up.
It just show the $user_id
because you have this code :
echo($user_id);
And you should have some controls on user inputs ! hackers can easily do some Sql injections.
for example use :
mysqli_real_escape_string($input);

Upload images PHP to MySQL and retrieve correct images to each form

I have to re-explain my situation they said.
my html form:
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="carform.css"/>
<head>
</head>
<body>
<form action="connection.php" method="post" class="smart-green">
<h1> Car listing
<span>Please fill all the text in the fields.</span>
</h1>
<label>
<span>Make:</span>
<select id="category">
<option value="Alfa Romeo">Alfa Romeo</option>
<option value="Aston Martin">Aston Martin</option>
<option value="Audi">Audi</option>
<option value="Austin">Austin</option>
<option value="Bentley">Bentley</option>
<option value="BMW">BMW</option>
<option value="Buddy">Buddy</option>
<option value="Buick">Buick</option>
<option value="Cadillac">Cadillac</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Chrysler">Chrysler</option>
<option value="Citroen">Citroen</option>
<option value="Dacia">Dacia</option>
<option value="Daewoo">Daewoo</option>
<option value="Daihatsu">Daihatsu</option>
<option value="Dodge">Dodge</option>
<option value="Ferrari">Ferrari</option>
<option value="Fiat">Fiat</option>
<option value="Fisker">Fisker</option>
<option value="Ford">Ford</option>
<option value="GMC">GMC</option>
<option value="Honda">Honda</option>
<option value="Hummer">Hummer</option>
<option value="Hyundai">Hyundai</option>
<option value="Infiniti">Infiniti</option>
<option value="Isuzu">Isuzu</option>
<option value="Iveco">Iveco</option>
<option value="Jaguar">Jaguar</option>
<option value="Jeep">Jeep</option>
<option value="Kewet">Kewet</option>
<option value="Kia">Kia</option>
<option value="Koenigsegg">Koenigsegg</option>
<option value="Lada">Lada</option>
<option value="Lamborghini">Lamborghini</option>
<option value="Lancia">Lancia</option>
<option value="Land Rover">Land Rover</option>
<option value="Lexus">Lexus</option>
<option value="Lincoln">Lincoln</option>
<option value="Lotus">Lotus</option>
<option value="Maserati">Maserati</option>
<option value="Maybach">Maybach</option>
<option value="Mazda">Mazda</option>
<option value="McLaren">McLaren</option>
<option value="Mercedes-Benz">Mercedes-Benz</option>
<option value="Mercury">Mercury</option>
<option value="MG">MG</option>
<option value="MINI">MINI</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Morgan">Morgan</option>
<option value="Morris">Morris</option>
<option value="Nissan">Nissan</option>
<option value="Oldsmobile">Oldsmobile</option>
<option value="Opel">Opel</option>
<option value="Peugeot">Peugeot</option>
<option value="Piaggio">Piaggio</option>
<option value="Plymouth">Plymouth</option>
<option value="Pontiac">Pontiac</option>
<option value="Porsche">Porsche</option>
<option value="Renault">Renault</option>
<option value="Reva">Reva</option>
<option value="Rolls Royce">Rolls Royce</option>
<option value="Rover">Rover</option>
<option value="Saab">Saab</option>
<option value="Seat">Seat</option>
<option value="Skoda">Skoda</option>
<option value="Smart">Smart</option>
<option value="Ssangyong">Ssangyong</option>
<option value="Subaru">Subaru</option>
<option value="Suzuki">Suzuki</option>
<option value="Tazzari">Tazzari</option>
<option value="Tesla">Tesla</option>
<option value="Think">Think</option>
<option value="Toyota">Toyota</option>
<option value="Triumph">Triumph</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Volvo">Volvo</option>
<option value="Others">Others</option>
</select>
</label>
<label>
<span>Title:</span>
<input id="title" type="text" name="title">
</label>
<label>
<span>Price:</span>
<input id="price" type="text" name="price">$
</label> <label>
<span>Description:</span>
<textarea id="description" name="description" rows="20" cols="60"></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" value="Send"/>
</label>
</form>
</body>
</html>
and my php:
<?php
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to mysqli: ".mysqli_connect_error();
}
$cat = mysqli_real_escape_string($con, $_POST['category']);
$tit = mysqli_real_escape_string($con, $_POST['title']);
$pri = mysqli_real_escape_string($con, $_POST['price']);
$des = mysqli_real_escape_string($con, $_POST['description']);
$sql = "INSERT INTO forsale (Category, Title, Price, Description) VALUES ('$cat', '$tit', '$pri', '$des')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
echo "1 record added";
mysqli_close($con);
?>
Now what I am missing is having uploading maximum of 12 images for this form per Id and being able to retrieve all the images that belongs to eash posting
your code if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
you are missing a closing curly brace like this, not sure if this is your onle problem but it certainly is a problem.
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));}

Getting Option Value with PHP

Below is a snippet of the code I have created on a form page. The form inputs display the correct variables for $adjustedGrossIncome, $personalExemptions and the calculation for Tax Due Before Refundable Credits, but when I try to echo $birthYear the php dies. Any hints what I'm doing wrong?
<?php
$filingStatus = $_Post['filingStatus'];
$birthYear = $_Post['birthYear'];
$dependents = $_Post['dependents'];
$children = $_POST['children'];
$exemptions = $_POST['exemptions'];
$income = $_POST['income'];
$investments = $_POST['investments'];
$retirement = $_POST['retirement'];
$property = $_POST['property'];
$adjustedGrossIncome = $income + $investments + $retirement;
$personalExemptions = ($exemptions * 3950);
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset style="width:500px;">
<legend>Filing Status</legend>
<p>
<label>
<input type="radio" name="filingStatus" value="married" id="filingStatus" />
Married filing jointly</label>
<br />
<label>
<input type="radio" name="filingStatus" value="single" id="filingStatus" />
Single or Head of Household</label>
</p>
</fieldset>
<p>
<label>Birth Year
<select name="birthYear" id="birthYear">
<option>Select Year</option>
<option value="1923">1923</option>
<option value="1924">1924</option>
<option value="1925">1925</option>
<option value="1926">1926</option>
<option value="1927">1927</option>
<option value="1928">1928</option>
<option value="1929">1929</option>
<option value="1930">1930</option>
<option value="1931">1931</option>
<option value="1932">1932</option>
<option value="1933">1933</option>
<option value="1934">1934</option>
<option value="1935">1935</option>
<option value="1936">1936</option>
<option value="1937">1937</option>
<option value="1938">1938</option>
<option value="1939">1939</option>
<option value="1940">1940</option>
<option value="1941">1941</option>
<option value="1942">1942</option>
<option value="1943">1943</option>
<option value="1944">1944</option>
<option value="1945">1945</option>
<option value="1946">1946</option>
<option value="1947">1947</option>
<option value="1948">1948</option>
<option value="1949">1949</option>
<option value="1950">1950</option>
<option value="1951">1951</option>
<option value="1952">1952</option>
<option value="1953">1953</option>
<option value="1954">1954</option>
<option value="1955">1955</option>
<option value="1956">1956</option>
<option value="1957">1957</option>
<option value="1958">1958</option>
<option value="1959">1959</option>
<option value="1960">1960</option>
<option value="1961">1961</option>
<option value="1962">1962</option>
<option value="1963">1963</option>
<option value="1964">1964</option>
<option value="1965">1965</option>
<option value="1966">1966</option>
<option value="1967">1967</option>
<option value="1968">1968</option>
<option value="1969">1969</option>
<option value="1970">1970</option>
<option value="1971">1971</option>
<option value="1972">1972</option>
<option value="1973">1973</option>
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
</select>
</label>
</p>
<p>
<label>Number of Dependents (not including taxpayer and spouse if applicable
<input type="text" name="dependents" id="dependents" />
</label>
</p>
<p>
<label>Number of Children under 18
<input type="text" name="children" id="children" />
</label>
</p>
<p>
<label>Total number of exemptions
<input type="text" name="exemptions" id="exemptions" />
</label>
</p>
<p>
<label>Total Wages/Salary
<input type="text" name="income" id="income" />
</label>
</p>
<p>
<label>Investment income (interest, dividends, capital gains)
<input type="text" name="investments" id="investments" />
</label>
</p>
<p>
<label>Retirement income (pension, IRA, 401(k), etc.)
<input type="text" name="retirement" id="retirement" />
</label>
</p>
<p>
<label>Annual property taxes paid on primary residence and/or annual rent
<input type="text" name="property" id="property" />
</label>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Calculate Net Tax Liability for 2013" />
<input type="reset" name="reset" id="reset" value="Reset Form" onClick="window.location.reload()" />
</p>
</form>
<p>Adjusted Gross Income: $<?php echo($adjustedGrossIncome); ?></p>
<p>Personal Exemptions: $<?php echo($personalExemptions); ?></p>
<p>Tax Due Before Refundable Credits: $<?php echo(($adjustedGrossIncome - $personalExemptions) * .0425); ?></p>
<p><?php echo($birthYear); ?></p>
Change all $_Post to uppercase $_POST
This is known as a superglobal and must be in uppercase.
More on superglobals can be found on the PHP.net Web site
http://php.net/manual/en/language.variables.superglobals.php

dynamically adding sets of form elements n times

i'm working on an application that lets users enter the opening hours for a restaurant. i have the form code set up like this:
<div class="hourswrapper">
Days:
<input type="checkbox" name="day1[]" value="1" />M
<input type="checkbox" name="day1[]" value="2" />Tu
<input type="checkbox" name="day1[]" value="3" />W
<input type="checkbox" name="day1[]" value="4" />Th
<input type="checkbox" name="day1[]" value="5" />F
<input type="checkbox" name="day1[]" value="6" />Sa
<input type="checkbox" name="day1[]" value="7" />Su
<br />
Opening Time:
<select name="starthour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
so basically the person chooses a set of hours and marks which days those apply to. of course there might be multiple sets of hours (ex. if a restaurant is open 12-2 one day but 12-5 another). i'd like to have a "Add More Hours" button that duplicates this same code when pressed, however many times is necessary.
what's the best way to go about this? how do you dynamically add a form element without needing to have it hidden at the beginning? how do i set different numbered names for each set of hours inputs, and later know how many to access (using PHP post)? i assume i'll need a javascript function that keeps track of how many have been added.
alternatively, i am open to suggestions on how to present hours inputs differently/better, perhaps a way that doesn't require dynamic creation of form elements. i do want to restrict the user's inputs so i don't have to worry about parsing weird entries, which is why i don't want to just have an text input box for each day.
thanks.
Checkout http://www.w3schools.com/dom/dom_node.asp
Particularly the cloneNode and appendChild methods
Probably best to use JavaScript/JQuery to dynamically add new elements, then use foreach in your PHP script to process all the inputs?
Hope this helps.
Y.J.
Here's a quick little example of duplicating the hours input based on which checkbox is checked. Each hours div gets its' own unique id (see the attr('id',hrsDivID) part of the code below). Mimic this to rename your inputs/selects. See http://jquery.com/ and http://api.jquery.com/ for more.
<head>
<script type="text/javascript" src="/shared/javascript/jquery.js"></script>
</head>
<div id="hours" style='display:none'>
Opening Time:
<select name="starthour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
Days:
<div id='day1'>
<input type="checkbox" name="day1[]" value="1" onClick="addHours('day1');" >M
</div>
<div id='day2'>
<input type="checkbox" name="day1[]" value="2" onClick="addHours('day2');" />Tu
</div>
<div id='day3'>
<input type="checkbox" name="day1[]" value="3" onClick="addHours('day3');" />W
</div>
<div id='day4'>
<input type="checkbox" name="day1[]" value="4" onClick="addHours('day4');" />Th
</div>
<div id='day5'>
<input type="checkbox" name="day1[]" value="5" onClick="addHours('day5');" />F
</div>
<div id='day6'>
<input type="checkbox" name="day1[]" value="6" onClick="addHours('day6');"/>Sa
</div>
<div id='day7'>
<input type="checkbox" name="day1[]" value="7" onClick="addHours('day7');"/>Su
</div>
<script>
function addHours(divID){
var d = document.getElementById(divID);
var hrsDivID = divID+'_hours_div';
$('#hours').clone().attr('id',hrsDivID).css('display','').appendTo(d);
return false;
}
</script>

Categories