I can't seem to find my PHP variable $quote_date. I have a form that grabs job records from the database and displays a job's current progress in the form ready to be edited, submitted and updated to the database. All of the job's other records are being collected and displayed correctly but the $quote_date is missing in action. Yet when I echo the $quote_date after the renderForm() function is executed it appears to really exist, at least at that point. Why is the $quote_date not being displayed in the form?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Allow the user to both create new records and edit existing records.
// Connect to the database.
$connect = mysqli_connect('localhost', 'username', 'password', 'database');
if ( !$connect ) {
die( 'connect error: '.mysqli_connect_error() );
}
// creates the new/edit record form.
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($error = '', $id = '', $start_date = '', $company = '', $stock_code = '', $card_quantity = '', $fiske_print = '', $carrier_quantity = '', $quoted = '', $quote_details = '', $quoted_date = '', $quote_accepted = '', $quote_accepted_date = '', $proof_sent = '', $proof_sent_date = '', $proof_approved = '', $proof_approved_date = '', $printed = '', $print_date = '', $closed_loop_allocated = '', $invoiced = '', $invoiced_date = '', $posted = '', $tracking_number = '', $postal_date = '', $paid = '', $is_bulk_load = '', $bulk_funds_recieved = '', $cards_loaded = '', $notes = '', $completed = '')
{
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
$pageName = 'overview';
?>
<?php
include('header.php');
?>
<h1><?php
if ($id != '') {
echo "Edit Record";
} else {
echo "New Record";
}
?></h1>
<?php
if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>";
}
?>
<form action="" method="post">
<div>
<table>
<tr>
<td colspan="2" style="text-align:center;"><strong>Job Details</strong></td>
<td colspan="2" style="text-align:center;"><strong>Job Progress</strong></td>
</tr>
<tr>
<td>ID: </td>
<td><input type="text" name="id" value="<?php echo $id; ?>" readonly></td>
<td>Quoted: </td>
<td><input type="checkbox" name="quoted" value="1" <?php if($quoted == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Start Date: </td>
<td><input type="date" name="start_date" value="<?php if($start_date !== ''){echo date('Y-m-d',strtotime($start_date));} ?>"></td>
<td>Quote Details: </td>
<td><input type="text" name="quote_details" size="40" value="<?php echo $quote_details; ?>"></td>
</tr>
<tr>
<td>Company: </td>
<td><input type="text" name="company" size="40" value="<?php echo $company; ?>"></td>
<td>Quote Date: </td>
// Here is where the mystery lies why is my $quote_date variable missing?
<td><input type="date" name="quote_date" value="<?php echo date('Y-m-d',strtotime($quote_date)); ?>"></td><?php echo '<script type="text/javascript">alert("'.$quote_date.'");</script>'; ?>
</tr>
<tr>
<td>Stock Code: </td>
<td>
<div id="billdesc">
<select id="test" name="stock_code">
<option class="non" value="GS01">GS01</option>
<option class="non" value="GS03">GS03</option>
<option class="non" value="SM01">SM01</option>
<option class="non" value="SM11">SM11</option>
<option class="non" value="CG01">CG01</option>
<option class="non" value="CG38">CG38</option>
<option class="editable" value="Other">Other</option>
</select>
<input class="editOption" style="display:none;" placeholder="Text juaj"></input>
</div>
</td>
<td>Quote Accepted: </td>
<td><input type="checkbox" name="quote_accepted" value="1" <?php if($quote_accepted == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Card Quantity: </td>
<td><input type="text" name="card_quantity" value="<?php echo $card_quantity; ?>"></td>
<td>Quote Accepted Date: </td>
<td><input type="date" name="quote_accepted_date" value="<?php if($quote_accepted_date !== ''){echo date('Y-m-d',strtotime($quote_accepted_date));} ?>"></td>
</tr>
<tr>
<td>Carrier Quantity: </td>
<td><input type="text" name="carrier_quantity" value="<?php echo $carrier_quantity; ?>"></td>
<td>Proof Sent: </td>
<td><input type="checkbox" name="proof_sent" value="1" <?php if($proof_sent == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Fiske Print: </td>
<td><input type="checkbox" name="fiske_print" value="1" <?php if($fiske_print == 1){echo 'checked';} ?>></td>
<td>Proof Sent Date: </td>
<td><input type="date" name="proof_sent_date" value="<?php if($proof_sent_date !== ''){echo date('Y-m-d',strtotime($proof_sent_date));} ?>"></td>
</tr>
<tr>
<td rowspan="6" colspan="2" style="text-align:center;">
Notes:<br>
<textarea name="notes" rows="8" cols="70"><?php echo $notes; ?></textarea>
</td>
<td style="text-align:right;">Proof Approved: </td>
<td style="text-align:left;"><input type="checkbox" name="proof_approved" value="1" <?php if($proof_approved == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Proof Approved Date: </td>
<td><input type="date" name="proof_approved_date" value="<?php if($proof_approved_date !== ''){echo date('Y-m-d',strtotime($proof_approved_date));} ?>"></td>
</tr>
<tr>
<td>Printed: </td>
<td><input type="checkbox" name="printed" value="1" <?php if($printed == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Print Date</td>
<td><input type="date" name="printed_date" value="<?php if($print_date !== ''){echo date('Y-m-d',strtotime($print_date));} ?>"></td>
</tr>
<tr>
<td>Closed Loop Allocated: </td>
<td><input type="checkbox" name="closed_loop_allocated" value="1" <?php if($closed_loop_allocated == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Invoiced: </td>
<td><input type="checkbox" name="invoiced" value="1" <?php if($invoiced == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Paid: </td>
<td><input type="checkbox" name="paid" value="1" <?php if($paid == 1){echo 'checked';} ?>></td>
<td>Invoice Date: </td>
<td><input type="date" name="invoice_date" value="<?php if($invoice_date !== ''){echo date('Y-m-d',strtotime($invoice_date));} ?>"></td>
</tr>
<tr>
<td>Is Bulk Load: </td>
<td><input type="checkbox" name="is_bulk_load" value="1" <?php if($is_bulk_load == 1){echo 'checked';} ?>></td>
<td>Posted: </td>
<td><input type="checkbox" name="posted" value="1" <?php if($posted == 1){echo 'checked';} ?>></td>
</tr>
<tr>
<td>Bulk Funds Recieved</td>
<td><input type="checkbox" name="bulk_funds_recieved" value="1" <?php if($bulk_funds_received == 1){echo 'checked';} ?> ></td>
<td>Postal Date: </td>
<td><input type="date" name="postal_date" value="<?php if($postal_date !== ''){echo date('Y-m-d',strtotime($postal_date));} ?>"></td>
</tr>
<tr>
<td>Cards Loaded: </td>
<td><input type="checkbox" name="cards_loaded" value="1" <?php if($cards_loaded == 1){echo 'checked';} ?>></td>
<td>Tracking Number: </td>
<td><input type="text" name="tracking_number" size="30" value="<?php echo $tracking_number; ?>"></td>
</tr>
<tr>
<td colspan="4" style="text-align:center;">
Completed: <input type="checkbox" name="completed" value="1" <?php if($completed == 1){echo 'checked';} ?>>
<input type="submit" name="submit" value="Save" style="width:90px" />
</td>
</tr>
</table>
<script type="text/javascript">
var initialText = $('.editable').val();
$('.editOption').val(initialText);
$('#test').change(function(){
var selected = $('option:selected', this).attr('class');
var optionText = $('.editable').text();
if(selected == "editable"){
$('.editOption').show();
$('.editOption').keyup(function(){
var editText = $('.editOption').val();
$('.editable').val(editText);
$('.editable').html(editText);
});
}else{
$('.editOption').hide();
}
});
</script>
</body>
</html>
<?php
}
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit an existing record
if (isset($_GET['id'])) {
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit'])) {
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id'])) {
// get the form data
// I'll get to this later... get form to display first...
echo 'We are saving a new edit of job ' . $id;
}
// if the 'id' variable is not valid, show an error message
else {
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else {
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0) {
// get 'id' from URL
$id = $_GET['id'];
// get the record from database
if ($stmt = $connect->prepare("SELECT id, start_date, company, stock_code, card_quantity, fiske_print, carrier_quantity, quoted, quote_details, quoted_date, quote_accepted, quote_accepted_date, proof_sent, proof_sent_date,proof_approved, proof_approved_date, printed, print_date, closed_loop_allocated, invoiced, invoiced_date, posted, tracking_number, postal_date, paid, is_bulk_load, bulk_funds_received, cards_loaded, notes, completed FROM jobs WHERE id = ?")) {
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($id,$start_date,$company,$stock_code,$card_quantity,$fiske_print,$carrier_quantity,$quoted,$quote_details,$quote_date,$quote_accepted,$quote_accepted_date,$proof_sent,$proof_sent_date,$proof_approved,$proof_approved_date,$printed,$printed_date,$closed_loop_allocated,$invoiced,$invoice_date,$posted,$tracking_number,$postal_date,$paid,$is_bulk_load,$bulk_funds_received,$cards_loaded,$notes,$completed);
/* fetch values */
while ($stmt->fetch()) {
printf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", $id,$start_date,$company,$stock_code,$card_quantity,$fiske_print,$carrier_quantity,$quoted,$quote_details,$quote_date,$quote_accepted,$quote_accepted_date,$proof_sent,$proof_sent_date,$proof_approved,$proof_approved_date,$printed,$printed_date,$closed_loop_allocated,$invoiced,$invoice_date,$posted,$tracking_number,$postal_date,$paid,$is_bulk_load,$bulk_funds_received,$cards_loaded,$notes,$completed);
}
// show the form by executing renderForm()
renderForm(NULL, $id,$start_date,$company,$stock_code,$card_quantity,$fiske_print,$carrier_quantity,$quoted,$quote_details,$quote_date,$quote_accepted,$quote_accepted_date,$proof_sent,$proof_sent_date,$proof_approved,$proof_approved_date,$printed,$printed_date,$closed_loop_allocated,$invoiced,$invoice_date,$posted,$tracking_number,$postal_date,$paid,$is_bulk_load,$bulk_funds_received,$cards_loaded,$notes,$completed);
// check to see if we have a quote date..?
echo 'quote date: '.$quote_date;
$stmt->close();
}
// show an error if the query has an error
else {
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view.php page
else {
header("Location: addJob.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else {
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit'])) {
// get the form data
$start_date = date("Y-m-d", strtotime($_POST['start_date']));
$company = $_POST['company'];
$stock_code = $_POST['stock_code'];
$card_quantity = $_POST['card_quantity'];
$carrier_quantity = $_POST['carrier_quantity'];
$fiske_print = $_POST['fiske_print'];
$quoted = $_POST['quoted'];
$quote_details = $_POST['quote_details'];
$quote_date = date("Y-m-d", strtotime($_POST['quote_date']));
$quote_accepted = $_POST['quote_accepted'];
$quote_accepted_date = date("Y-m-d", strtotime($_POST['quote_accepted_date']));
$proof_sent = $_POST['proof_sent'];
$proof_sent_date = date("Y-m-d", strtotime($_POST['proof_sent_date']));
$proof_approved = $_POST['proof_approved'];
$proof_approved_date = date("Y-m-d", strtotime($_POST['proof_approved_date']));
$printed = $_POST['printed'];
$printed_date = date("Y-m-d", strtotime($_POST['printed_date']));
$closed_loop_allocated = $_POST['closed_loop_allocated'];
$invoiced = $_POST['invoiced'];
$invoice_date = date("Y-m-d", strtotime($_POST['invoice_date']));
$posted = $_POST['posted'];
$postal_date = date("Y-m-d", strtotime($_POST['postal_date']));
$tracking_number = $_POST['tracking_number'];
$paid = $_POST['paid'];
$is_bulk_load = $_POST['is_bulk_load'];
$bulk_funds_received = $_POST['bulk_funds_received'];
$cards_loaded = $_POST['cards_loaded'];
$completed = $_POST['completed'];
/* Prepare an insert statement */
$query = "INSERT INTO jobs (start_date,company,stock_code,card_quantity,fiske_print,carrier_quantity,quoted,quote_details,quoted_date,quote_accepted,quote_accepted_date,proof_sent,proof_sent_date,proof_approved,proof_approved_date,printed,print_date,closed_loop_allocated,invoiced,invoiced_date,posted,tracking_number,postal_date,paid,is_bulk_load,bulk_funds_received,cards_loaded,notes,completed) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stmt = mysqli_prepare($connect, $query);
mysqli_stmt_bind_param($stmt, "sssssssssssssssssssssssssssss", $start_date,$company,$stock_code,$card_quantity,$fiske_print,$carrier_quantity,$quoted,$quote_details,$quote_date,$quote_accepted,$quote_accepted_date,$proof_sent,$proof_sent_date,$proof_approved,$proof_approved_date,$printed,$printed_date,$closed_loop_allocated,$invoiced,$invoice_date,$posted,$tracking_number,$postal_date,$paid,$is_bulk_load,$bulk_funds_received,$cards_loaded,$notes,$completed);
/* Execute the statement */
mysqli_stmt_execute($stmt);
/* close statement */
mysqli_stmt_close($stmt);
// redirect the user
header("Location: index.php");
}
// if the form hasn't been submitted yet, show the form
else {
error_log('SQL error ('.__FILE__.' line '.__LINE__.'): '. $connect->error);
renderForm();
}
}
// close the mysqli connection
$connect->close();
?>
I have a simple form on my page and I want to make sure every field is filled before inserting the values into the database. The problem is the condition never met, even if every field is filled I still get "Something is missing"...
Search:
<form method="post" action="<?php echo $_SERVER[" PHP_SELF "]?>">
<input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
<?php if (isset($_POST[ "search"])) { $email_search=m ysql_real_escape_string($_POST[ "email_search"]); $check=m ysql_query( "SELECT * FROM torzsvendegek WHERE email = '$email_search'"); $s=m ysql_fetch_array($check); }?>
<form method="post" action="<?php echo $_SERVER[" PHP_SELF "]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td>
<input type="text" name="email" value="<?php echo $email_search;?>" disabled>
</td>
</tr>
<tr>
<td>Név:</td>
<td>
<input type="text" name="nev" value="<?php echo $s['nev'];?>">
</td>
</tr>
<tr>
<td>Mikor:</td>
<td>
<input type="text" name="mikor">
</td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td>
<input type="text" name="ejszakak">
</td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($s[ 'nyelv']=="magyar" ) echo "selected=\"selected\ ""; ?>>Magyar</option>
<option value="nemet" <?php if($s[ 'nyelv']=="nemet" ) echo "selected=\"selected\ ""; ?>>Német</option>
<option value="lengyel" <?php if($s[ 'nyelv']=="lengyel" ) echo "selected=\"selected\ ""; ?>>Lengyel</option>
<option value="roman" <?php if($s[ 'nyelv']=="roman" ) echo "selected=\"selected\ ""; ?>>Román</option>
<option value="szlovak" <?php if($s[ 'nyelv']=="szlovak" ) echo "selected=\"selected\ ""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s[ 'nyelv']=="egyeb" ) echo "selected=\"selected\ ""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td>
<textarea name="megjegyzes">
<?php echo htmlspecialchars($s[ 'megjegyzes']);?>
</textarea>
</td>
</tr>
</table>
<br>
<table width="440">
<tr>
<td>
<input type="submit" name="submit_add" value="Hozzáad">
</td>
</tr>
</table>
</form>
<?php if (isset($_POST[ "submit_add"]) && !empty($_POST[ "nev"]) && !empty($_POST[ "email"]) && !empty($_POST[ "mikor"]) && !empty($_POST[ "ejszakak"])){ $nev=m ysql_real_escape_string($_POST[ "nev"]); $email=m ysql_real_escape_string($_POST[ "email"]); $mikor=m ysql_real_escape_string($_POST[ "mikor"]); $ejszakak=m ysql_real_escape_string($_POST[ "ejszakak"]); $nyelv=m ysql_real_escape_string($_POST[ "nyelv"]); $megjegyzes=m ysql_real_escape_string($_POST[ "megjegyzes"]); $check2=m ysql_query( "SELECT * FROM torzsvendegek WHERE email = '$email'"); $br="<br>" ; if (mysql_num_rows($check2)> 0) { $adatok = mysql_fetch_array($check2); $osszesen = ($adatok['ejszakak'] + $ejszakak); mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'"); echo "
<br>".$email." Updated"; } else { mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')"); echo "
<br>".$email." Added"; } } else { echo "Something is missing"; } ?>
You made 2 mistakes in your code:
1) You didn't close the first form (missing </form>)
2) You disabled the E-mail input field which resulted in always empty
Here's the updated (though deprecated and insecure!!!) code:
<?php
if(isset($_POST["search"])){
$email_search = mysql_real_escape_string($_POST["email_search"]);
$check = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email_search'");
$s = mysql_fetch_array($check);
}
if(isset($_POST["submit_add"]) && !empty($_POST["nev"]) && !empty($_POST["email"]) && !empty($_POST["mikor"]) && !empty($_POST["ejszakak"])){
$nev = mysql_real_escape_string($_POST["nev"]);
$email = mysql_real_escape_string($_POST["email"]);
$mikor = mysql_real_escape_string($_POST["mikor"]);
$ejszakak = mysql_real_escape_string($_POST["ejszakak"]);
$nyelv = mysql_real_escape_string($_POST["nyelv"]);
$megjegyzes = mysql_real_escape_string($_POST["megjegyzes"]);
$check2 = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email'");
$br = "<br>";
if (mysql_num_rows($check2) > 0){
$adatok = mysql_fetch_array($check2);
$osszesen = ($adatok['ejszakak'] + $ejszakak);
mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'");
echo "<br>".$email." Updated";
} else {
mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')");
echo "<br>".$email." Added";
}
} else {
echo "Something is missing";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
Search: <input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?php echo $email_search;?>"></td>
</tr>
<tr>
<td>Név:</td>
<td><input type="text" name="nev" value="<?php echo $s['nev'];?>"></td>
</tr>
<tr>
<td>Mikor:</td>
<td><input type="text" name="mikor"></td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td><input type="text" name="ejszakak"></td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($s['nyelv']=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($s['nyelv']=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($s['nyelv']=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($s['nyelv']=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($s['nyelv']=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s['nyelv']=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td><textarea name="megjegyzes"><?php echo htmlspecialchars($s['megjegyzes']);?></textarea></td>
</tr>
</table>
<br>
<table width="440">
<tr>
<td><input type="submit" name="submit_add" value="Hozzáad"></td>
</tr>
</table>
</form>
You need to close your search form tag to keep the two forms separated
Search:<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>"><input placeholder="e-mail address" type="text" name="email_search"><input type="submit" name="search" value="Go"></form>
and
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr><td>E-mail:</td><td><input type="text" name="email" value="<?php echo $email_search;?>" disabled></td></tr>
<tr><td>Név:</td><td><input type="text" name="nev" value="<?php echo $s['nev'];?>"></td></tr>
<tr><td>Mikor:</td><td><input type="text" name="mikor"></td></tr>
<tr><td>Éjszakák száma:</td><td><input type="text" name="ejszakak"></td></tr>
<tr><td>Nemzetisége:</td><td align="left"> <select name="nyelv">
<option value="magyar" <?php if($s['nyelv']=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($s['nyelv']=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($s['nyelv']=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($s['nyelv']=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($s['nyelv']=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s['nyelv']=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select></td></tr>
<tr><td>Megjegyzés:</td><td><textarea name="megjegyzes"><?php echo htmlspecialchars($s['megjegyzes']);?></textarea></td></tr>
</table><br>
<table width="440"><tr><td><input type="submit" name="submit_add" value="Hozzáad"></td></tr></table>
</form>
You did not close your search form and you need to remove the disabled attribute from your email input field.
I re-wrote your code to help you will debugging. I commented out all the stuff related to the database so you can focus on the form fields only. Here is the code I re-wrote. I left comments so you can see what I did.
<?php
/******JUST TO MAKE DEBBUGGING EASIER***/
echo "<pre>"; // Start of the pre> tags
/**ANYTHING TO DO WITH THE DATABASE I HAVE COMMENTED OUT**/
if(isset($_POST["search"])){
$email_search = mysql_real_escape_string($_POST["email_search"]);
/**PRINT_R FOR DEBUGGING PURPOSES, REMOVE!!*/
print_r($email_search);
//$check = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email_search'");
//$s = mysql_fetch_array($check);
}
/***CHECK THE POST DATA, REMOVE FROM APPLICATION ONCE YOU HAVE DEBUGGED THE DATA**/
print_r($_POST);
/***I WILL STORE THE POST DATA IN VARIABLES BEFORE CHECKING**/
$nev = isset($_POST["nev"]) ? mysql_real_escape_string($_POST["nev"]) : null;
$email = isset($_POST["email"]) ? mysql_real_escape_string($_POST["email"]) : null;
$mikor = isset($_POST["mikor"]) ? mysql_real_escape_string($_POST["mikor"]) : null;
$ejszakak = isset($_POST["ejszakak"]) ? mysql_real_escape_string($_POST["ejszakak"]) : null;
$nyelv = isset($_POST["nyelv"]) ? mysql_real_escape_string($_POST["nyelv"]) : null;
$megjegyzes = isset($_POST["megjegyzes"]) ? mysql_real_escape_string($_POST["megjegyzes"]) : null;
if(isset($_POST["submit_add"]) && !is_null($nev) && !is_null($email) && !is_null($mikor) && !is_null($ejszakak)){
/*******
SINCE I ALREADY HAVE THEM, YOU NEED TO REMOVE THEM FROM THE CODE
$nev = mysql_real_escape_string($_POST["nev"]);
$email = mysql_real_escape_string($_POST["email"]);
$mikor = mysql_real_escape_string($_POST["mikor"]);
$ejszakak = mysql_real_escape_string($_POST["ejszakak"]);
$nyelv = mysql_real_escape_string($_POST["nyelv"]);
$megjegyzes = mysql_real_escape_string($_POST["megjegyzes"]);
******/
//$check2 = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email'");
$br = "<br>";
/*********
if (mysql_num_rows($check2) > 0) {
$adatok = mysql_fetch_array($check2);
$osszesen = ($adatok['ejszakak'] + $ejszakak);
mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'");
echo "<br>".$email." Updated";
}else {
mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')");
echo "<br>".$email." Added";
}
****/
}elseif (isset($_POST["submit_add"])) {
echo "Something is missing";
}
echo '</pre>';//end of pre
?>
Search:
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?php echo $email;?>" ></td>
</tr>
<tr>
<td>Név:</td>
<td><input type="text" name="nev" value="<?php echo $nev;?>"></td>
</tr>
<tr>
<td>Mikor:</td>
<td><input type="text" name="mikor" value="<?php echo $mikor;?>"></td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td><input type="text" name="ejszakak" value="<?php echo $ejszakak;?>"></td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($nyelv=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($nyelv=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($nyelv=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($nyelv=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($nyelv=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($nyelv=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td><textarea name="megjegyzes"><?php echo htmlspecialchars($megjegyzes);?></textarea>
</td>
</tr>
</table><br>
<table width="440">
<tr><td><input type="submit" name="submit_add" value="Hozzáad"></td></tr>
</table>
</form>
I have a script below which the user logs a php form.
There is a section with 2 drop down menus next to each other, 'npc_battery_n_code' and 'npc_battery_other_code'.
What I am trying to do is to make it so one of these drop down menus has to have an option selected for the form to be submitted, but not both or none.
So for example, if an option gets selected in 'npc_battery_n_code' and then the user selects another option in 'npc_battery_other_code', then the value of the 'npc_battery_n_code' will revert to the 'None' option.
Any suggestions welcome
<?php //logsale.php
require_once ('./includes/config.inc.php');
$page_title = 'Log a sale';
include ('./includes/header.html');
if (!isset($_SESSION['sp_user_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/login.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}
$users = $_SESSION['sp_user_id'];
?>
<h1>Heading</h1>
<?php
if (isset($_POST['submitted'])) {// Handle the form.
require_once ('mydatabase.php');
if (!empty($_POST['npc_quantity'])) {
$quantity = escape_data($_POST ['npc_quantity']);
} else {
$quantity = FALSE;
echo '<p><font color="red">You forgot to enter the Quantity</font></p>';
}
if (!empty($_POST['npc_customer_name'])) {
$customer = escape_data($_POST ['npc_customer_name']);
} else {
$customer = FALSE;
echo '<p><font color="red">You forgot to enter the Customer Name</font></p>';
}
if (!empty($_POST['npc_registration_no'])) {
$rego = escape_data($_POST ['npc_registration_no']);
} else {
$rego = FALSE;
echo '<p><font color="red">You forgot to enter the Customer Rego Number</font></p>';
}
if (!empty($_POST['npc_suo_no'])) {
$suo = escape_data($_POST ['npc_suo_no']);
} else {
$suo = FALSE;
echo '<p><font color="red">You forgot to enter the SUO Number</font></p>';
}
if (!empty($_POST['npc_amb_club_no'])) {
$repair_order = escape_data($_POST ['npc_amb_club_no']);
} else {
$repair_order = FALSE;
echo '<p><font color="red">You forgot to enter the A-Club number</font></p>';
}
if (!empty($_POST['npc_repair_order'])) {
$amb_club = escape_data($_POST ['npc_repair_order']);
} else {
$amb_club = FALSE;
echo '<p><font color="red">You forgot to enter the Repair Order</font></p>';
}
if (!empty($_POST['npc_invoice_no'])) {
$inv = escape_data($_POST ['npc_invoice_no']);
} else {
$inv = FALSE;
echo '<p><font color="red">You forgot to enter the Invoice Number </font></p>';
}
if (!empty($_POST['npc_entry_userdate'])) {
$inv_date = escape_data($_POST ['npc_entry_userdate']);
} else {
$inv_date = FALSE;
echo '<p><font color="red">You forgot to enter the Invoice Date</font></p>';
}
if ($quantity && $customer && $rego && $suo && $repair_order && $amb_club && $inv && $inv_date) {
$uid = #mysql_insert_id(); //Get the url ID.
$query = "INSERT INTO npc_sales_list
(npc_item_id , sp_user_id, npc_battery_n_code, npc_battery_other_code, npc_quantity,
npc_customer_name, npc_registration_no, npc_suo_no, npc_amb_club_no, npc_repair_order,
npc_entry_userdate, npc_usertype, npc_points, npc_bonus_points, npc_entry_date)
VALUES
('$uid', '$users', '$_POST[npc_battery_n_code]', '$_POST[npc_battery_other_code]', '$quantity',
$customer , $rego, $suo, $amb_club , $repair_order,
$inv , 'NPC', '5' , '0' , NOW())";
$result = mysql_query ($query) or trigger_error
("Query: $query\n<br />MySQL Error: " .mysql_error());
if ($result = #mysql_query($query)) {
//Finish the page.
echo '<p>The sale with invoice number: <strong>' . $inv . '</strong> <br />
is now registered into the system.</p>
<p>If you would like to log in further sales, please click here</p>
<p>If you would like to view your sales log, please click here</p>
';
include ('./includes/footer.html'); // Include the HTML footer.
exit();
} else { // If it did not run OK.
echo '<p><font color="red" size="+1">You could not be
registered due to a system error. We apologize for any
inconvenience.</font></p>';
}
}
} else { // If one of the data tests failed.
echo '<p><font color="red" size="+1">Please check all manatory fields are complete and try again.
</font></p>';
}
mysql_close(); // Close the database connection.
?>
<form enctype="multipart/form-data" action="logsale.php" method="post">
<table width="520" border="0" cellspacing="3" cellpadding="2">
<tr>
<td width="184"><div align="right">NPC Vehicle<span class="style2">*</span></div></td>
<td width="323"><select name="npc_battery_n_code" id="npc_battery_n_code">
<option value="None"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == 'None')
{echo 'selected="selected"';} ?>>None
</option>
<option value="55D23L"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23L')
{echo 'selected="selected"';} ?>>55D23L
</option>
<option value="55D23R"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '55D23R')
{echo 'selected="selected"';} ?>>55D23R
</option>
<option value="75D23R"
<?php if (isset($_POST['npc_battery_n_code']) && $_POST['npc_battery_n_code'] == '75D23R')
{echo 'selected="selected"';} ?>>75D23R
</option>
</select></td>
</tr>
<tr>
<td width="184"><div align="right">Other <span class="style2">*</span></div></td>
<td width="323">
<select name="npc_battery_other_code" id="npc_battery_other_code">
<option value="50D20L"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '50D20L')
{echo 'selected="selected"';} ?>>50D20L
</option>
<option value="50D20R"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '50D20R')
{echo 'selected="selected"';} ?>>50D20R
</option>
<option value="55D23LMF"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '55D23LMF')
{echo 'selected="selected"';} ?>>55D23LMF
</option>
<option value="55D23RMF"
<?php if (isset($_POST['npc_battery_other_code']) && $_POST['npc_battery_other_code'] == '55D23RMF')
{echo 'selected="selected"';} ?>>55D23RMF
</option>
</select></td>
</tr>
<tr>
<td><div align="right">Quantity <span class="style2">*</span></div></td>
<td><input type="text" name="npc_quantity" size="10" maxlength="10"
value="<?php if (isset($_POST['npc_quantity'])) echo $_POST['npc_quantity']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Customer name<span class="style2">*</span></div></td>
<td><input type="text" name="npc_customer_name" size="30" maxlength="30"
value="<?php if (isset($_POST['npc_customer_name'])) echo $_POST['npc_customer_name']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Rego number <span class="style2">*</span></div></td>
<td><input type="text" name="npc_registration_no" size="10" maxlength="7"
value="<?php if (isset($_POST['npc_registration_no'])) echo $_POST['npc_registration_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">SUO No.<span class="style2">*</span></div></td>
<td><input type="text" name="npc_suo_no" size="10" maxlength="7"
value="<?php if (isset($_POST['npc_suo_no'])) echo $_POST['npc_suo_no']; ?>"/></td>
</tr>
<tr>
<td><div align="right">A-Club ID<span class="style2">*</span></div></td>
<td><input type="text" name="npc_amb_club_no" size="15" maxlength="7"
value="<?php if (isset($_POST['npc_amb_club_no'])) echo $_POST['npc_amb_club_no']; ?>"/></td>
</tr>
<tr>
<td><div align="right">Repair Order <span class="style2">*</span></div></td>
<td><input type="text" name="npc_repair_order" size="15" maxlength="12"
value="<?php if (isset($_POST['npc_repair_order'])) echo $_POST['npc_repair_order']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Invoice No. <span class="style2">*</span></div></td>
<td><input size="10" name="npc_invoice_no"
value="<?php if (isset($_POST['npc_invoice_no'])) echo $_POST['npc_invoice_no']; ?>" /></td>
</tr>
<tr>
<td><div align="right">Invoice Date <span class="style2">*</span></div></td>
<td><input size="12" name="npc_entry_userdate" maxlength="10"
value="<?php if (isset($_POST['npc_entry_userdate'])) echo $_POST['npc_entry_userdate']; ?>"/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></p><input type="hidden" name="submitted" value="TRUE" />
</td>
</tr>
</table>
</form>
<p>Footer</p>
<?php
include ('./includes/footer.html');
?>
jquery: select and unselect items of listbox is what you are looking for.
How you want your script to work cannot be achieved with PHP or Mysql. You can only work with the data when the website is loaded. If you want to edit the data without refreshing the page you need to use JavaScript, or more advanced jQuery.