Use php to insert drop select form element - php

I am attempting to create a drop down box with php that pulls states from an array that is populated with state names. I have saved my file with the .php extension and have added code to attempt to create a drop down menu from my array that I am creating. For some reason, the drop down menu is not working.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script type="text/javascript">
//create function to be called upon submission
function verify(event) {
var hasError = false;
var check1=document.getElementById("saws").value;
var check2=document.getElementById("pliers").value;
var check3=document.getElementById("planers").value;
var ok = [];
ok[0] = check1.search(/^$|\d+/);
ok[1] = check2.search(/^$|\d+/);
ok[2] = check3.search(/^$|\d+/);
for (i = 0; i < ok.length; i++) {
if (ok[i] !== 0) {
hasError = true;
alert("Invalid input.");
event.preventDefault();
}
}
if (hasError) return false;
else return true;
}
</script>
<title>l6p2</title>
<meta charset="UTF-8" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<h1>Lab 6, part 2 </h1>
<form action = "/formtest.php" method="post" onsubmit= "return verify(event)">
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>Saw</td>
<td>$15.99</td>
<td><input type="text" name="saws" id="saws" size="2" /></td>
</tr>
<tr>
<td>Pliers</td>
<td>$12.99</td>
<td><input type="text" name="pliers" id="pliers" size="2" /></td>
</tr>
<tr>
<td>Planer</td>
<td>$79.99</td>
<td><input type="text" name="planers" id="planers" size="2" /></td>
</tr>
</tbody>
</table>
<p>
<label> First Name:
<input type="text" name="fname" size ="30" />
</label>
<br />
<br />
<label> Last Name:
<input type="text" name="lname" size ="30" />
</label>
<br />
<br />
<label> Shipping Address:
<input type="text" name="address" size ="30" />
</label>
<br />
<br />
<label> City:
<input type="text" name="csz" size ="30" />
</label>
<br />
<br />
<?php
$states = array('Alabama','Alaska','Arizona','California','Florida','Georgia');
echo '<label> State:';
echo '<select name="state">';
for ($i = 0; $i < count($states); $i++) {
echo '<option>' . $states[$i] . '</option>';
}
echo '</select>';
echo '</label>';
?>
</p>
<h3>Payment Method</h3>
<p>
<label>
<input type="radio" name="payment" value="visa" checked = "checked" />
Visa
</label>
<label>
<input type="radio" name="payment" value="mc" />
Master Card
</label>
<label>
<input type="radio" name="payment" value="amex" />
American Express
</label>
<br />
</p>
<p>
<input type="Submit" value="Submit Order" />
</p>
</form>
<h3>Links</h3>
Home
</body>
</html>
This is the part that is not working correctly:
<?php
$states = array('Alabama','Alaska','Arizona','California','Florida','Georgia');
echo '<label> State:';
echo '<select name="state">';
for ($i = 0; $i < count($states); $i++) {
echo '<option>' . $states[$i] . '</option>';
}
echo '</select>';
echo '</label>';
?>

Just use a simple foreach in this case:
foreach ($states as $state) {
echo '<option value="'.$state.'">' . $state . '</option>';
}
Your array is basic and doesn't need any special magic to make it work.

I think that the problem is the count on the loop, you need to know the array length, so try the sizeof($states) instead.
for ($i = 0; $i < sizeof($states); $i++) {
Another thing: you had to put some value on the "option" to get that on the back-end side. Like this:
echo '<option value="'.$states[$i].'">' . $states[$i] . '</option>';
And the last: You don`t need to put the "select" inside the "label". You can use the "for" attribute on the label to reference the select id.
This is all the code:
<?php
$states = array('Alabama','Alaska','Arizona','California','Florida','Georgia');
echo '<label for="state"> State: </label>';
echo '<select name="state" id="state">';
for ($i = 0; $i < sizeof($states); $i++) {
echo '<option value="'.$states[$i].'">' . $states[$i] .
'</option>';
}
echo '</select>';
?>
Good luck. :)

Related

How to insert multiple inputs with same input name

Assume that i have table: transactions with 7 columns:id, date, amount, detail, type, purpose, location. I want to be able to input 3 rows once i click on submit button. In my confiq.php there is connection set up in variable $query.
This is the code that I have in my insertmultiple.php (later it will be included in my overview.php in a table as 3 rows).
Problem: I want to insert 3 lines of data from html form into new rows in table. Do yousee where I make mistake? I have a feeling its somewhere where I start condition if.
Thanks to everyone.
<?php
include('config.php');
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type)
VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if ( $sql === TRUE) {
mysqli_query ($query,$sql);
} else {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>
After preparing the mysqli_connect in ($CONNECTION):
$CONNECTION = mysqli_connect("IP_ADDRESS", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DBNAME");
then:
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type)
VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if ( !(mysqli_query($CONNECTION, $sql) === TRUE) ) {
echo "Error: " . mysqli_error($CONNECTION) . "<br>" . $sql->error;
}
}
Your test is wrong, $sql will never be true, it is a string. Try this :
<?php
include('config.php');
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if (mysqli_query ($query,$sql) === FALSE) {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>
EDIT : I'm assuming config.php defines $query var and open the connection. That being said, you should really use PDO and prepared statements to avoid SQL injection.
try this code:
note: read the comments
<?php
include('config.php');
//added an if statment to check if the user have pressed submit (<input type="submit" **name="submit"** id="submit_button" style="width: 50px">)
// this will insure that the query wont start unless data has been sent
if (isset($_POST['submit'])) {
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach ($date as $key => $d) {
$sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
// if ($sql === TRUE) { //the $sql will not be true it will be the equal to to the string of the query
// mysqli_query($query, $sql);
// } else {
// echo "Error: " . $sql . "<br>" . $query->error;
// }
if (!mysqli_query($query, $sql)) { //tries to perform the query, if it doesnt work prints the error
echo "Error: " . $sql . "<br>" . $query->error;
}
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>
Hi there, good example. I had the same problem and there is a difference between grant all privileges on *.* to root#localhost identified by 'password' and grant all privileges on *.* to root#'%' identified by 'password'
if you are using root on localhost you might as well just wanna run the grant all privileges on *.* to root#localhost identified by 'password'
For Your HTML and PHP scrip have
<?php
include 'db_config.php';
if (isset($_POST['date']) && isset($_POST['detail']) && isset($_POST['type'])) {
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
for ($i = 0; $i < count($date); $i++) {
$sql = "INSERT INTO transcations (date, detail, type) " .
" VALUES ('$date[$i]', '$detail[$i]', '$type[$i]')";
//$conn is the name of your connection string
$results = mysqli_query($conn, $sql);
if (!$results) {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post" action="">
<table>
<tr>
<td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr><td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr>
<td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</td>
</tr>
</table>
</form>
</body>
</html>

Using php for abbreviating the states in a drop down menu

I'm back once again, I know you guys are probably tired of me lol but there are somethings I figured out how to add the text boxes. I am still having trouble with the drop down menu. I am supposed to have a drop down menu that has the list of the states and when you submit it, it will be abbreviated but that doesn't want to work for me either. I'm pretty sure it is something easy to fix and I keep over looking it.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php"
onsubmit="return validateForm()"
method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text"
name="firstname" id="firstname"
size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text"
name="lastname" id="lastname"
size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text"
name="streetaddress" id="streetaddress"
size="35" />
</label>
<br>
<br>
<label>City
<input type="text"
name="city" id="city"
size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</label>
<br>
<br>
<label>Zip code:
<input type="text"
name="zipcode" id="zipcode"
size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment"
id="pref_payment_visa" value="visa" checked />
</label><br>
<label>MasterCard
<input type="radio" name="pref_payment"
id="pref_payment_master" value="master" checked />
</label><br>
<label>American Express
<input type="radio" name="pref_payment"
id="pref_payment_american" value="american" checked />
</label><br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
Try this:
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
?>
</table>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_id; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
</label>
That is why code indentation is so important.. You were missing closing PHP tags at two places. Refer to below code.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php" onsubmit="return validateForm()" method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0)
{
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult))
{
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text" name="firstname" id="firstname" size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text" name="lastname" id="lastname" size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</label>
<br>
<br>
<label>City
<input type="text" name="city" id="city" size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult_state = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult_state))
{
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
} ?>
</select>
<?php } ?> <!-- remove this -->
</label>
<br>
<br>
<label>Zip code:
<input type="text" name="zipcode" id="zipcode" size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment" id="pref_payment_visa" value="visa" checked />
</label>
<br>
<label>MasterCard
<input type="radio" name="pref_payment" id="pref_payment_master" value="master" checked />
</label>
<br>
<label>American Express
<input type="radio" name="pref_payment" id="pref_payment_american" value="american" checked />
</label>
<br>
<input type="submit" value="Submit!">
</form>
</body>
</html>

php calculation not correct

I have gotten rid of the errors with the help of others but for some reason the calculations are not going through, does anyone have any thoughts?
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your reservation for <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> room/s</h1>
<h1>And a room type of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>
<?php
if (isset($_POST['pets']))
{
echo 'With Pets';
}
if (isset($_POST['breakfast']))
{
echo ' and Breakfast Buffet';
}
?>
<h1/>
<h1>Your total due will be: <?php echo $_POST['$total']; ?> </h1>
<p> </p>
<h1>
<?php
if ($_POST['numberofrooms'] == "1")
{
$subtotal = ($_POST['quantity'] * 250);
}
elseif ($_POST['numberofrooms'] == "2")
{
$subtotal = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$subtotal = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$subtotal = ($_POST['quantity'] * $breakfast);
}
$smoke = 500;
if ($_POST['smokingpreference'] == 'smoking')
{
$subtotal = $subtotal + $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$total = $subtotal + $pets;
}
}
?>
</h1>
</body>
</html>
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- $250.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- $350.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- $425.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add $500.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add $200.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add $25.00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
I cannot seem to wrap my head around how I should go about getting my outputs, as my syntax is fine, but I keep getting errors. Obviously the user needs to be able to select any combination and get the correct output, but I cannot even find the cause of the error as syntactically it's fine. I will add the html portion as well.
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- $250.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- $350.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- $425.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add $500.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add $200.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add $25.00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
<!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>Vacation Rental Response Form</title>
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your Reservation For <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> Room/s</h1>
<h1>With A Room Type Of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
<h1>And <?php echo $_POST['breakfast']; ?> </h1>
<h1>Your Total Due Will Be: <?php echo $_POST['$sub1']; ?> </h1>
<p> </p>
<h1>
<?php
if (isset($_POST['numberofrooms']) == "1")
{
$sub1 = ($_POST['quantity'] * 250);
}
elseif (isset($_POST['numberofrooms']) == "2")
{
$sub2 = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$sub3 = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$sub1 = $sub1 * $breakfast;
$sub2 = $sub2 * $breakfast;
$sub3 = $sub3 * $breakfast;
}
$smoke = 500;
if (isset($_POST['smokingpreference']))
{
$sub1 = $sub1 * $smoke;
$sub2 = $sub2 * $smoke;
$sub3 = $sub3 * $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$sub1 = $sub1 * $pets;
$sub2 = $sub2 * $pets;
$sub3 = $sub3 * $pets;
}
}
?>
</h1>
</body>
</html>
Your problem is that your are trying to access indexes in your $_POST array that are no there:
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
The element for pets is a checkbox, if it is not checked, you will not get a pets element in $_POST. So something like this would be better:
<h1>With/Without <?php echo isset($_POST['pets']) ? 'With' : 'Without'; ?> </h1>

show data in search without using submit button

Does anyone know how to search and show data without using submit button..because thats the only function i need to finish my code..can anyone help me? I dont want to use submit button cause it refreshes the page...I have the code for search already I need a function that after I search the data will show in the textboxes without using submit button.
php code:
<?php
if($_GET){
include('include/connect.php');
$batchcode = $_GET['code'];
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode = '".$batchcode."' ");
if($sql) {
while($rows = mysql_fetch_array($sql)){
$aic[] = $rows['aic'];
$name[] = $rows['name'];
$address[] = $rows['address'];
}
}
}else{
$aic = array(NULL,NULL,NULL,NULL);
$name = array(NULL,NULL,NULL,NULL);
$address = array(NULL,NULL,NULL,NULL);
}
?>
html code:
<html>
<head>
<title>test</title>
</head>
<body>
<form action="" method="get">
Search Batchcode:<input type="text" name="code" id="query" /><br />
<table>
<tr>
<td>
aic: <br />
<input type="text" name="optA1" value="<?php if(empty($aic[0])){$aic[0] = array(NULL);}else{echo $aic[0];} ?>" /> <br />
<input type="text" name="optA2" value="<?php if(empty($aic[1])){$aic[1] = array(NULL);}else{echo $aic[1];} ?>" /> <br />
<input type="text" name="optA3" value="<?php if(empty($aic[2])){$aic[2] = array(NULL);}else{echo $aic[2];} ?>" /> <br />
<input type="text" name="optA4" value="<?php if(empty($aic[3])){$aic[3] = array(NULL);}else{echo $aic[3];} ?>" /> <br />
</td>
<td>
Name List: <br />
<input type="text" name="optB1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" /> <br />
<input type="text" name="optB2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" /> <br />
<input type="text" name="optB3" value="<?php if(empty($name[2])){$name[2] = array(NULL);}else{echo $name[2];} ?>" /> <br />
<input type="text" name="optB4" value="<?php if(empty($name[3])){$name[3] = array(NULL);}else{echo $name[3];} ?>" /> <br />
</td>
<td>
Address: <br />
<input type="text" name="optC1" value="<?php if(empty($address[0])){$address[0] = array(NULL);}else{echo $address[0];} ?>" /> <br />
<input type="text" name="optC2" value="<?php if(empty($address[1])){$address[1] = array(NULL);}else{echo $address[1];} ?>" /> <br />
<input type="text" name="optC3" value="<?php if(empty($address[2])){$address[2] = array(NULL);}else{echo $address[2];} ?>" /> <br />
<input type="text" name="optC4" value="<?php if(empty($address[3])){$address[3] = array(NULL);}else{echo $address[3];} ?>" /> <br />
</td>
</form>
</body>
</html>
script code:
<!--search function code-->
<script type="text/javascript">
$(document).ready(function(){
$("#query").autocomplete({
source : 'search.php',
select : function(event,ui){
$("#query").html(ui.item.value);
}
});
});
</script>
search.php page code:
<?php
$q = $_GET['term'];
mysql_connect("localhost","root","");
mysql_select_db("test");
$query = mysql_query("SELECT DISTINCT batchcode FROM tb_app WHERE batchcode LIKE '$q%'");
$data = array();
while($row = mysql_fetch_array($query)){
$data[]=array('value'=>$row['batchcode']);
}
echo json_encode($data);
?>
In your html you need to create a link or a simple button and give it a class or id.
Then in javascript add click event listener and perform an ajax call.
search here
And in your javascript
$( "#mysubmit" ).click(function() {
// read query field value
// Perform ajax call
});
Hope this is going to help you.
You can also do:
$( "#query" ).change(function() {
var sendMe = $.post("search.php");
sendMe.done(function(data) {
$( "#query" ).html(data);
});
});

Textbox problem with onclick #

I'm just trying to display a table in client side from database as a distinct value from every column. Here i have fetched the value by using "select distinct ...." code and put this value in a single textbox
here is the code...
<!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>Untitled Document</title>
<script type="text/javascript">
function onClick()
{
alert(document.form.thirdparty.value);
}
</script>
</head>
<body>
<p> </p>
<form name="form" method="get" >
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('database',$con);
$res = mysql_query("SELECT count(*) as count FROM tablename") or die(mysql_error());
while ($row = mysql_fetch_array($res)) {
$val=$row['count'];
} ?>
<input name="fino" type="text" id="fino" size="5" value="<?php echo $val." docs" ?>" style="border-style:hidden; color:#0083fa;" />
<table width="1474" height="270" border="0" >
<tr>
<td width="180" height="41">
<input name="Item" type="DocumentType" id="textfield5" value="    Item"size="30" /> </td>
<td width="180" height="41">
<label>
<td width="180" height="41"> <input type="text" name="textfield8" id="textfield8" style="visibility:hidden" /></td>
</label>
<label>
<td width="180" height="41"> <input type="text" name="textfield9" id="textfield9" style="visibility:hidden"/></td>
</label>
</td>
</tr>
<tr>
<td>
<?php
$a=mysql_query("select distinct language from tablename");
$c=0;
$i=1;
$x=1;
while($row = mysql_fetch_array($a))
{
$b=$row['language'];
$i=$b;
$d=mysql_query("select count(*) as count from tablename where language='$i' ");
while ($row = mysql_fetch_array($d)) {
$e=$row['count'];
$cal=round(($e/$val)*100);
}
?>
//in this textbox am using onclick function
<input type="text" size="30" style="height:<?php echo $cal.px?>"value="<?php echo "$i"." "." "." "."$e"." docs" ?>"name="language" id="textfield"onClick="onClick();" />
<?php
$i++;
$c++;
}
?> </td>
<td>
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('database',$con);
$a1=mysql_query("select distinct Subject from tablename");
$c1=0;
$i1=1;
while($row = mysql_fetch_array($a1))
{
$b1=$row['Subject'];
$i1=$b1;
$d1=mysql_query("select count(*) as count from tablename where Subject='$i1' ");
while ($row = mysql_fetch_array($d1)) {
$e1=$row['count'];
$cal1=round(($e1/$val)*100);
}
?>
<input type="text" size="30" style="height:<?php echo $cal1.px?>" value="<?php echo "$i1"." "." "." "."$e1"." docs" ?>"name="item" id="textfield2" />
<?php
$i++;
$c++;
}
?> </td>
<td>
<label>
<td> <input type="text" name="textfield12" id="textfield12" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield13" id="textfield13" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield14" id="textfield14" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield15" id="textfield15" style="visibility:hidden" /> </td>
</label>
</form>
</td>
</tr>
</table>
<blockquote>
<p align="center"> </p>
</blockquote>
</body>
</html>
EDIT:
The following is a sample where the value of the textbox is displayed in an alert box.
As the others have said, cannot suggest anything other than this without seeing code.
Example
<input type = 'text' id = 'myTextBox' onclick='alert(this.value)' />
First don't call your function onclick better name is something like LanguageClick.
Second, don't give same id to all elements.. if you don't have real use for it just omit the ID it won't break anything.
Third, proper event is onfocus as user can reach the textbox by pressing Tab key, which won't trigger the click event.
Code would look like this:
<input type="text" size="30" style="height:<?php echo $cal.px?>;" value="<?php echo "$i"." "." "." "."$e"." docs" ?>" name="language" onfocus="LanguageClick(this);" />
And the JavaScript code:
function LanguageClick(oTextbox) {
var sValue = oTextbox.value;
alert("You wrote: " + sValue);
}
As you pass this to the function, you have reference to the textbox element and you can read its value property.

Categories