I made a form in php. I am using 3 different forms in the document. So I have to distinguish these forms. Therefore I am using the submit "name-tag".
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if (isset($_POST['submit_1'])) {
....
}
if (isset($_POST['submit_2'])) {
....
}
if (isset($_POST['submit_3'])) {
....
}
}
This is how I check these different submit-buttons.
But I noticed that I need to use the click function in JS instead of the submit function (cause the form shouldn't be submitted with a page-reload)
$('.erzaehlcafe_submit').click(function(){
if ($('#year_from').val() == 'none' || $('#month_from').val() == 'none'
|| $('#day_from').val() == 'none' || $('#topic').val() == '' || $('#contributer').val() == ''
|| $('#begin').val() == '' || $('#place').val() == '' || $('#entrance').val() == '') {
$("#dialog_empty").dialog( "open" );
return false;
}
var form = $('#erzaehlcafe_add');
var data = form.serialize();
$.ajax({
url: "index.php?section=events",
type: "POST",
data: data,
success: function (reqCode) {
alert(reqCode);
if (reqCode == 1) {
//Date inserted into DB
$("#dialog_add_event").dialog( "open" );
} else {
$("#dialog_add_event_error").dialog( "open" );
}
}
});
return false; //dont submit form
});
So how to make a difference between these forms? (All of them are using same names for same input).
The only way to solve the problem is to define other name indexes for all input typed. Is there any other way?
Edit: Here is my template ( remember I used submit_1, submit_2 and submit_3 instead of erzaehlcafe_submit and so on to make it more precise)
<div id="event_accordion">
<h3>Erzählcafé hinzufügen</h3>
<div>
<form id="erzaehlcafe_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Datum:</td>
<td>
<select name="day_from" id="day_from">
<option value="none" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from" id="month_from">
<option value="none" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from" id="year_from">
<option value="none" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="topic" id="topic" /></td>
</tr>
<tr>
<td>Referent:</td>
<td><input type="text" name="contributer" id="contributer" /></td>
</tr>
<tr>
<td>Beginn:</td>
<td><input type="text" name="begin" id="begin" /> Uhr</td>
</tr>
<tr>
<td>Ort:</td>
<td><input type="text" name="place" id="place" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="entrance" id="entrance" /> €</td>
</tr>
</table>
<br />
<div id="add_erzaehlcafe">
<input type="submit" id="small" class="erzaehlcafe_submit" name="erzaehlcafe_submit" value="speichern">
</div>
</form>
</div>
<h3>Vortrag hinzufügen</h3>
<div>
<form name="vortrag_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Datum:</td>
<td>
<select name="day_from">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="name_topic" id="name_topic" /></td>
</tr>
<tr>
<td>Referent:</td>
<td><input type="text" name="name_contributer" id="name_contributer" /></td>
</tr>
<tr>
<td>Beginn:</td>
<td><input type="text" name="name_begin" id="name_begin" /> Uhr</td>
</tr>
<tr>
<td>Ort:</td>
<td><input type="text" name="name_place" id="name_place" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="name_entrance" id="name_entrance" /> €</td>
</tr>
</table>
<br />
<div id="add_vortrag">
<input type="submit" id="small" class="vortrag_submit" name="vortrag_submit" value="speichern">
</div>
</form>
</div>
<h3>Ausstellung hinzufügen</h3>
<div>
<form name="ausstellung_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Von:</td>
<td>
<select name="day_from">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Bis:</td>
<td>
<select name="day_till">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_till">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_till">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="name_topic" id="name_topic" /></td>
</tr>
<tr>
<td>Aussteller:</td>
<td><input type="text" name="name_contributer" id="name_contributer" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="name_entrance" id="name_entrance" /></td>
</tr>
</table>
<br />
<div id="add_ausstellung">
<input type="submit" id="small" class="ausstellung_submit" name="ausstellung_submit" value="speichern">
</div>
</form>
</div>
<h3>Termine bearbeiten/löschen</h3>
<div>
<button id="edit_event">Termin bearbeiten</button>
</div>
Add an <input type="hidden"> to each form with name="submit_X". The input type="submit" element seems to be ignored by serialize();.
Example:
<input type="text" id="year_from" name="year_from" />
<!-- More inputs.... -->
<input type="hidden" name="submit_1" value="true" />
You could also use JavaScript to add another key to the POST data.
Take a look at this fiddle: http://jsfiddle.net/cd7Yy/
You need to show your HTML too.
But from what I see, if you use OnClick event on buttons in your forms, you can create buttons in your HTML with different names for different forms, and then, in your PHP cheack which name you recieve in $_POST
Related
I have a HTML/PHP form that needs validation of each of the entered fields before updating MySQL database. It would be nice if this cold be done as each field is entered...however, I could use the form's action attribute to run a different validation script but then if there are errors I would need to return to the form and re-populate it with previously entered data for correction - not sure how I would do all this but I guess it's possible. However, I've seen a post here that shows how this can be done with a javascript function for the validation but is this possible to do in PHP? I'm just learning PHP so I'd hate to now have to also learn javascript :( Some guidance appreciated.
<?php
if(isset($_POST)) {
echo __LINE__ . " Form has been posted <br>";
echo __LINE__ . $_POST['state_city'];
}
?>
<form action="" method="post">
<table border="0" align="center">
<tr height="29px" width = "220px">
<td>
<label for="student_id"><b>Student-ID</b></label>
</td>
<td>
<input type="text" placeholder="Student ID" name="studentid" value="<?php echo $row['studentid'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px">
<td>
<label for="student_name"><b>Student name</b></label>
</td>
<td>
<input type="text" placeholder="Student name" name="name" value="<?php echo $row['$studentname'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px"><td><label for="start_date"><b>Start Date</b></label></td>
<td><input type="date" placeholder="Enter start date" name="start_date" value="<?php echo $row['startdate'];;?>" required></td>
</tr>
<tr height="29px"><td width = "220px">
<b>Student Grade</b></td>
<td>
<select name="sgrade" id="sgrade">
<option value="1" <?php if ($row['$sgrade'] == '1') echo "selected"; ?>>Grade 1<option>
<option value="2" <?php if ($row['$sgrade'] == '2') echo "selected"; ?>>Grade 2<option>
<option value="3" <?php if ($row['$sgrade'] == '3') echo "selected"; ?>>Grade 3<option>
<option value="4" <?php if ($row['$sgrade'] == '4') echo "selected"; ?>>Grade 4<option>
</select>
</td>
<tr>
// value?
<tr height="29px"><td width = "220px"><b>Country of Birth</b></td>
<td>
<select name="country" id="country">
<?php
$sql = "SELECT * FROM countries";
$result = mysqli_query($con, $sql);
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
</td>
</tr>
<tr height="29px" width = "220px">
<td><label for="state_city"><b>State and/or main city</b></label></td>
<td><input type="text" placeholder="Student State and/or Main City" name="state_city" value="<?php echo $row['$state_city'];?>" required></td>
</tr>
<tr height="29px" width = "220px">
<td><label for="healthcheck"><b>Checked?</b></label></td>
<td>
<input type="radio" id="checkyes" name="check" value="1" <?php if ($row['check']) echo "checked";?>>
<label for="checkyes"> Yes</label><br>
<input type="radio" id="checkno" name="check" value="0" <?php if (!$row['check']) echo "checked";?>>
<label for="checkno"> No</label><br><br>
</td>
</tr>
</table>
<button type="submit">Save</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="history.back()" value="Go back!" class="cancelbtn">Cancel</button>
</div>
</form>
<script>
function check() {
document.getElementById("checkyes").checked = true;
}
function uncheck() {
document.getElementById(hcheckyes").checked = false;
}
</script>
</body>
</html>
If you want it to be dynamic as the user enters the information you will have to use JavaScript as that would be a front-end solution. With PHP you can do you validation/sanitizing of the data on the server-side as the form is submitted. It is suggested you do both.
For your question of repopulating this is one way
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
For more info : click here
I have created a form which takes the data and inserts it in the database. onlick of create button i want the function to check database whether the entry with employee_id already exists. if exists i want it to display the data already exists do you still want to insert, i am noob in this can anybody help me with this. the form is
<form id="myForm" name="myForm" action='insert.php' method='post' >
<input type='hidden' name='st' value=0>
<table style="text-align:center; width:100%">
<tr>
<td style="text-align:right"><label>Select SE/AE:</label></td>
<td style="text-align:left">
<?php include("configs.php");
$sql = "SELECT DISTINCT seae FROM se_ae ";?>
<select name="seae">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['seae']; ?>">
<?php echo $row['seae']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Brand:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `brand` FROM se_ae ";?>
<select name="brand">
<option value="" selected> </option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['brand']; ?>">
<?php echo $row['brand']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Territory:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `territory` FROM se_ae ";?>
<select name="territory">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['territory']; ?>">
<?php echo $row['territory']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="name">Employee Name:</label></td>
<td style="text-align:left">
<input type="text" id="name" name="name"/>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="number">Employee ID:</label></td>
<td style="text-align:left">
<input type="text" id="number" name="number" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="email"> Email:</label></td>
<td style="text-align:left">
<input type="text" id="email" name="email" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="contact"> Contact:</label></td>
<td style="text-align:left">
<input type="text" id="contact" name="contact" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="exist"> Exist:</label></td>
<td style="text-align:left">
<input type="radio" id="exist" name="exist" value="Active"/>Active
<input type="radio" id="exist" name="exist" value="NA"/>NA
</td>
</tr>
<tr>
<td style="text-align:right" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="Reset" type="reset" value="Reset">
</td>
<td style="text-align:left" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="submit" type="submit" value="Create">
</td>
</tr>
</table>
</form>
You can use the Jquery validation
$(function () {
$("#resturantRegistration").validate({
rules: {
number: {
required: true,
remote:"user/checkEmployee"
}
},
messages: {
number: {
required: "Please enter Employee id",
remote: $.validator.format("Employee id already in use")
}
}
});
});
PHP function
function checkEmployee(){
$emailid = $_GET['number'];
if(!empty($emailid)){
$emailRes = $this->users->is_email_available($emailid);
if ($emailRes == ''){
echo 'false';
} else {
echo 'true';
}
}
}
Model function to check with database
/**
* Check if email available for registering
*
* #param string
* #return bool
*/
function is_email_available($email)
{
$this->db->select('1', FALSE);
$this->db->where('LOWER(email)=', strtolower($email));
$this->db->or_where('LOWER(new_email)=', strtolower($email));
$query = $this->db->get($this->table_name);
return $query->num_rows() == 0;
}
above code is follow the codeigniter MVC framework you can customize this in core PHP as well
I have the following code in the view of an application I am developing using the codeigniter framework
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></form></td>
</tr>
<?php } ?>
Have made edit to the code based on the answers below now it is like this
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
But the output is still the same
However when the view is loaded, the code comes out like this
<tr>
<form action="http://sms-dev.anovatesoft.com/index.php/MainController/AttendanceSet" method="post" accept-charset="utf-8"></form>
<input type="hidden" name="child_nric" value="A12">
<input type="hidden" name="teachernric" value="T001">
<input type="hidden" name="progcode" value="FEE001">
<td>A12</td>
<td>
<select name="attendance">
<option value="1" selected="">Present</option>
<option value="0" selected="">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
Notice the difference in the </from> position? In my code I have it after the submit button but when it renders it closes just after the <form> opens making the form useless as I am not able to submit it.
What is the reason behind this strange behavior? And how do I fix it?
The View
<?php
$this->load->helper('form');
?>
<script>
function ListView(){
//if($("#id").val() != "0"){
var formURL = "<?php echo base_url();?>MainController/Attendance/"+$("#program").val();
$.post(formURL).done(function(data){$("#body_view_paste").html(data); });
}
function fire(){
alert("I have been fired");
}
</script>
<div align="center">
<table align="center" style="max-width:80%">
<tr>
<td>Program</td>
<td>
<select id="program" name="program" onchange="ListView()">
<option value="0">Select</option>
<?php
$nricno = $_SESSION['username'];
$sql = "SELECT distinct programs.activities, programs.progcode FROM events
LEFT JOIN programs ON programs.progcode=events.progcode WHERE events.teacher_nric='$nricno'";
$activities = $this->db->query($sql);
foreach($activities->result_array() as $row){?>
<option value="<?php echo $row['progcode'];?>"><?php echo $row['activities'];?></option>
<?php } ?>
</select>
</td>
</tr>
</table>
</div>
<div>
<?php if(isset($program)){
$selected=3;
$query = $this->db->query("SELECT child_nric FROM child_reg_prog WHERE progcode = '$program'");
?>
<table>
<thead>
<tr>
<td>NRIC NO</td>
<td>Attendance</td>
<td>Action</td>
</tr>
</thead>
<?php foreach($query->result_array() as $row){ ?>
<tr>
<?php echo form_open('MainController/AttendanceSet');?>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close();} ?>
</table>
<?php } ?>
</div>
The Controller part
public function Attendance($program = "")
{
$this->main_model->pagePermissions("Attendance");
if ($program == "") {
$this->load->view('Attendance');
} else {
$data['program'] = $program;
$this->load->view('Attendance', $data);
}
}
public function AttendanceSet()
{
$child_nric = $_POST['child_nric'];
$attendance = $_POST['attendance'];
$teachernric = $_POST['teachernric'];
$progcode = $_POST['progcode'];
$ispresent="No";
if($attendance==1)
{
$ispresent="Yes";
}
$this->load->model("Attendance_model");
$this->Attendance_model->insert($progcode, $childnric, $teachernric, $ispresent);
$this->load->view('Attendance');
}
The model part
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attendance_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function insert($progcode, $childnric, $teachernric, $ispresent)
{
$sql = "INSERT INTO attendence (progcode, childnric, teachernric, ispresent ) VALUES (?,?,?,?) ";
$query = $this->db->query($sql, array($progcode, $childnric, $teachernric, $ispresent));
}
}
?>
try this:
<?php foreach($query->result_array() as $row){ ?>
<?php echo form_open('MainController/AttendanceSet');?>
<tr>
<input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
<input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
<input type="hidden" name="progcode" value="<?php echo $program; ?>"/>
<td><?php echo $row['child_nric']; ?></td>
<td>
<select name="attendance">
<option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
<option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
</select>
</td>
<td> <input type="submit" name="submit" value="Submit"></td>
</tr>
<?php echo form_close(); ?>
<?php } ?>
Both of the below statements should come outside the foreach loop
<?php echo form_open('MainController/AttendanceSet');?>
<?php echo form_close(); ?>// you need to close it
My Selectbox Items from aircraft table
<select name="selectaircraft">
<option id="0">-- Select Aircraft -- </option>
<?php
require("dbc.php");
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
<option id="<?php echo $viewallaircraft['ID']; ?>">
<?php echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
hi guys im trying to use the the value of selectaircraft to search and display the result to textbox.. here is my code so far
<?php
require("dbc.php");
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE 'PR200'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig "value="<?php echo $viewallconfig['config']; ?>" />
<?php } ?>
As you can see, I only put PR200 to search, my question is how can i use the select value instead? I also want the search query to happen everytime i click the select box.
guys can some1 help me? i know how to code this in VB6 it will be like this
assume : select name or in VB6 combobox = selectbox
Private Sub Combo3_Click()
adodc1.recorsource ="Select * from aircrafttable where aircraft = " & selectbox & "'"
adodc1.refresh
Text1.Text = Adodc1.Recordset("aircraft")
everytime i click combo/selectbox the query will perform and give different answer to textbox.
i dont know how to do this in PHP pls help!
Edited Added all codes
<form action="addrecord.php" method="post" >
<table border=1>
<tr><td bgcolor="#999999">
<strong>Area:</strong> </td>
<td>
<input type="text" required="required" name="aarea" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Aircraft:</strong></td>
<td>
<form method="post" action="aircraft.php">
<select name="selectaircraft" onchange="this.form.submit();">
<option id="0">Aircraft</option>
<?php
require("dbc.php");
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
<option id="<?php echo $viewallaircraft['ID']; ?>">
<?php
echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
<?php
require("dbc.php");
if(isset($_POST)){
$takeaircraft = mysql_real_escape_string($_POST['selectaircraft']);
{
?>
<input type="text" name="aaircraft" size=3 value="<?php echo $takeaircraft; ?>" />
<?php }
}?>
</form>
</td></tr>
<tr><td bgcolor="#999999">
<strong>Flight:</strong> </td>
<td><input type="text" name="aflight" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Configuration:</strong> </td>
<td>
<?php
require("dbc.php");
if(isset($_POST)){
$selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$selectaircraft."'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig" value="<?php echo $viewallconfig['config']; ?>" />
<?php }
}?>
<br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Month:</strong> </td>
<td>
<select name="amonth">
<option value="na">Month</option>
<option value="January">January</option>
</select>
</td></tr>
<tr><td bgcolor="#999999">
<strong>Frequency:</strong> </td>
<td><span style="font-size:11px; font-weight:bolder" >
Mon<input type="checkbox" id='check1' onClick='checkmon()'>
<input type="hidden" id="txt1" name="hiddenmon" value="n/a">
Tue<input type="checkbox" id='check2' onClick='checktue()'>
<input type="hidden" id="txt2" name="hiddentue" value="n/a">
Wed<input type="checkbox" id='check3' onClick='checkwed()'>
<input type="hidden" id="txt3" name="hiddenwed" value="n/a">
Thu<input type="checkbox" id='check4' onClick='checkthu()'>
<input type="hidden" id="txt4" name="hiddenthu" value="n/a">
Fri<input type="checkbox" id='check5' onClick='checkfri()'>
<input type="hidden" id="txt5" name="hiddenfri" value="n/a">
Sat<input type="checkbox" id='check6' onClick='checksat()'>
<input type="hidden" id="txt6" name="hiddensat" value="n/a">
Sun<input type="checkbox" id='check7' onClick='checksun()'>
<input type="hidden" id="txt7" name="hiddensun" value="n/a">
</span>
<br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Menu:</strong> </td>
<td><input type="text" name="amenu" size=10 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Cycle:</strong> </td>
<td>
<select name="acycle">
<option value="na">Cycle</option>
<option value="Cycle 1">Cycle 1</option>
</select>
<tr><td bgcolor="#999999">
<strong>Items:</strong> </td>
<td>
<select name="aitem">
<option value="na">Items</option>
</select>
<tr><td bgcolor="#999999">
<strong>STD Time:</strong> </td>
<td><input type="text" name="astdtime" size=5 /><br /></td></tr>
<tr><td bgcolor="#999999">
<strong>Quantity:</strong> </td>
<td><input type="text" name="aqty" size=5 /><br /></td></tr>
<tr>
<td colspan="2" align="right" bgcolor="#999999">
<input type='submit' name="add" value="Add Records" />
</td>
</tr>
</table>
</form>
trigger submit on change of select box as below
<form action="path/to/your/query/file.php" method="post"> //if query in same page leave your action blank
<select name="selectaircraft" onchange="this.form.submi();">
<option id="0">-- Select Aircraft -- </option>
<?php
require("dbc.php");
$selected_aircraft=isset($_POST['selectaircraft'])?$_POST['selectaircraft']:"";
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
$selectstr="";
if($selected_aircraft==$viewallaircraft['aircraft'] ){
$selectstr="selected='selected'";
}
?>
<option id="<?php echo $viewallaircraft['ID']; ?>" <?php echo $selectstr ?> >
<?php
echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>
</form>
and change you query as below:
<?php
require("dbc.php");
if(isset($_POST)){
$selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '{ $selectaircraft}'");
while($viewallconfig= mysql_fetch_array($getallconfig)){
?>
<input type="text" name="aconfig "value="<?php echo $viewallconfig['config']; ?>" />
<?php }
}?>
Update updated code to maintain state of select box.
Try like this;
$aircraft = mysql_real_escape_string($_POST['selectaircraft']); //or $_GET
$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$aircraft."'");
You can keep the select box(1st) inside a form , on change submit the form to the next page .
And get the POST/GET value , and use it for select query for searching.
I want to display the selected price, category and size on the dropdown slideingDiv. But what I have done below is not working. I have try to echo out to see if the data have been sent thought but I got nothing. Is anything wrong with my code?
Script
<script type="text/javascript">
$(document).ready(function () {
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function () {
$(".slidingDiv").slideToggle();
});
});
</script>
Button
<button a href="product.php?ProdID=<?php echo $id; ?>" class="show_hide" id="button" name="button">Add to cart</a></button>
PHP
<?php
dbconnect();
if(isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && `isset($_POST['Category'])){`
$pid = $_POST['pid'];
$length = $_POST["length"];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];
echo $pid; // I have add this echo to see data have been passed though but i get nothing in return
echo $length;
$stmt4 = $conn->prepare("
SELECT Product.Name as ProductName, Category.Name, size, Price
FROM item_Product, Product, Category
WHERE Product.ProdID =:pid
AND size= :length AND Category.Name = :Category Limit 1");
$stmt4->bindParam('pid',$pid);
$stmt4->bindParam('length',$length);
$stmt4->bindParam('Category',$Category);
$stmt4->execute();
foreach ($stmt4->fetchAll(PDO::FETCH_ASSOC) as $row4 ) {
$product_name = $row4["ProductName"];
$price = $row4["Price"];
$length = $row4["size"];
$Category = $row4["Name"];
?>
Item was added shopping bag </br>
Name:<?php echo $row4['ProductName']; ?> </br>
Length:<?php echo $row4['size']; ?> </br>
Category:<?php echo $row4['Name']; ?> </br>
Price:<?php echo $row4['Price']; ?> </br>
<?php } }
?>
View Cart
</div>
TABLE
<td width="160">Price:</td>
echo '<td name="pricetag" id="pricetag">'.$row2['Price'].'</td>';
</tr>
<tr>
<td>Category</td>
<td>
<select id="Category" name="Category">
echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
</select>
</td>
</tr>
<tr>
<td width="160">Length:</td>
<td>
<select name="length" id="length">
<option SELECTED value="'.$row3['size'].'">'.$row3['size'].</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<div class="button">
<button class="show_hide" id="button" name="button">Add to cart</button>
</div>
Try this: SEE MY FIDDLE
HTML
<table>
<tr>
<td>Price</td>
<td>
<select id='priceTag'>
<option value='p1'>price 1</option>
<option value='p2'>price 2</option>
</select>
</td>
</tr>
<tr>
<td>Category</td>
<td>
<select id='Category'>
<option value='c1'>cat 1</option>
<option value='c2'>cat 2</option>
</select>
</td>
</tr>
<tr>
<td>Size</td>
<td>
<select id='Size'>
<option value='s1'>size 1</option>
<option value='s2'>size 2</option>
</select>
</td>
</tr>
</table>
<button id='button'>Submit</button>
<div id='selected'>
<table cellpadding="5" border='1'>
<tr>
<td>Price:</td>
<td id='sprice'></td>
</tr>
<tr>
<td>Category:</td>
<td id='scat'></td>
</tr>
<tr>
<td>Size:</td>
<td id='ssize'></td>
</tr>
</table>
</div>
JQuery
$(document).ready(function()
{
$('#selected').hide();
$('#button').click(function()
{
var price = $('#priceTag').val();
var cat = $('#Category').val();
var size = $('#Size').val();
$('#sprice').text(price);
$('#scat').text(cat);
$('#ssize').text(size);
$('#selected').slideDown();
});
});
Seems that you do not have a space in your <?php} ?>
Do something like <?php } ?>
Error:
<?php} ?>
try this,
<?php } ?> // add space between php and }
<?php}
should be
<?php }
Also
<button a href="product.php?ProdID=<?php echo $id; ?>" class="show_hide" id="button" name="button">Add to cart</a></button> is in correct
should be
<button>Add to cart</button>