I have a problem to show the 4 different data in the input fields. I have create the onchange function to get the 1 value in the input field, but how to get other different data to show in the other fields:
Below is my coding:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="New Category_value">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
echo '<option value="' . $rs_incharge['function_code'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Name:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_name" name="function_name" title="function_name">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="activity_code" name="activity_code" title="activity_code">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Name:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="activity_name" name="activity_name" title="activity_name">
</div>
</div>
<script>
function getComboA(selectObject) {
var value = selectObject.value;
document.getElementById("function_code").value =value;
document.getElementById("function_name").value =value;
document.getElementById("activity_code").value =value;
document.getElementById("activity_name").value =value;
}
<script>
Now my output show me the 4 same data in the input fields, like below the picture:
Output 1
Actually I want to show the 4 different data, now I just grab the first data using this code $rs_incharge['function_code'], how let these $rs_incharge['function_name'], $rs_incharge['activity_code']$rs_incharge['activity_name'] can show the different field, hope someone can guide me solve this problem. Thanks.
You should store the data in additional attributes in the <option> itself then get the values onchange and then perform your operation. This should help you.
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">New Category</option
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
// save the data here↓↓ in data attributes
echo "<option value='{$function_code_select}' data-act_code='{$activity_code_select}' data-act_name='{$activity_name_select}'>{$function_name_select}</option>";
}
?>
</select>
Then in your script
<script>
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
</script>
Below is the quick demo of what I am talking about-
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">New Category</option>
<!-- Provide default values here -->
<!-- After giving values via PHP your HTML code should look like this-->
<option value='Function Code 1' data-act_code='Activity Code 1' data-act_name='Activity Name 1'>Function Name 1</option>
<option value='Function Code 2' data-act_code='Activity Code 2' data-act_name='Activity Name 2'>Function Name 2</option>
<option value='Function Code 3' data-act_code='Activity Code 3' data-act_name='Activity Name 3'>Function Name 3</option>
</select>
Function Code: <input type="text" class="form-control" id="function_code" name="function_code" title="function_code" /><br>
Function Name: <input type="text" class="form-control" id="function_name" name="function_name" title="function_name" /><br>
Activity Code: <input type="text" class="form-control" id="activity_code" name="activity_code" title="activity_code" /><br>
Activity Name: <input type="text" class="form-control" id="activity_name" name="activity_name" title="activity_name" />
Related
Hello my favorite people!
I am trying to send an email after submitting a form, with the AUTO INCREMENT number attached to the email because the AUTO INCREMENT number is the clients Job Card Reference Number. So far i have successfully created the insert script which inserts the data into the database perfectly, and also sends the email too. But does not attach the AUTO INCREMENT number into the email. The INT(11) AUTO INCREMENT primary key is "job_number" in my MySQL database.
Here is my insert page:
<form action="addnewrepairprocess.php" method="POST">
<div class="form-group">
<label for="date">Date</label>
<input type="date" name="date" id="date" class="form-control" placeholder="Job Card Date">
</div>
<div class="form-group">
<label for="client_full_name">Client Full Name</label>
<input type="text" name="client_full_name" class="form-control" id="client_full_name" placeholder="Mr. Laptop Man">
</div>
<div class="form-group">
<label for="client_email">Client Email Address</label>
<input type="email" name="client_email" class="form-control" id="client_email" placeholder="example#live.co.za">
</div>
<div class="form-group">
<label for="client_phone">Client Phone Number</label>
<input type="text" name="client_phone" class="form-control" id="client_phone" placeholder="071 984 5522">
</div>
<div class="form-group">
<label for="item_for_repair">Item For Repair</label>
<select name="item_for_repair" id="item_for_repair">
<option value="Laptop">Laptop</option>
<option value="Desktop">Desktop</option>
<option value="Television">Television</option>
<option value="Washing Machine">Washing Machine</option>
<option value="Tumble Dryer">Tumble Dryer</option>
<option value="Dishwasher">Dishwasher</option>
<option value="Microwave">Microwave</option>
<option value="Fridge">Fridge</option>
<option value="Printer">Printer</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<label for="repair_description">Repair Description</label>
<input type="text" name="repair_description" class="form-control" id="repair_description" placeholder="Laptop is dead...">
</div>
<div class="form-group">
<label for="hardware_details">Hardware Details</label>
<input type="text" name="hardware_details" class="form-control" id="hardware_details" placeholder="Black Lenovo Laptop with Charger">
</div>
<div class="form-group">
<label for="diagnostic_fee">Diagnostic Fee</label>
<input type="text" name="diagnostic_fee" class="form-control" id="diagnostic_fee">
</div>
<div class="form-group">
<label for="tech_assigned">Technician Assigned</label>
<select name="tech_assigned" id="tech_assigned">
<option value="Not Assigned Yet">Not Assigned Yet</option>
<option value="Brendon">Brendon</option>
<option value="Gabriel">Gabriel</option>
<option value="Tapiwa">Tapiwa</option>
<option value="Conrad">Conrad</option>
</select>
</div>
<div class="form-group">
<label for="current_status">Current Status</label>
<select name="current_status" id="current_status">
<option value="Pending">Pending</option>
<option value="In Progress">In Progress</option>
<option value="On Hold Spares Required">On Hold Spares Required</option>
<option value="On Hold Other Fault">On Hold Other Fault</option>
<option value="Repair Completed">Repair Completed</option>
</select>
</div>
<div class="form-group">
<label for="technician_notes">Technician Notes</label>
<input type="text" name="technician_notes" class="form-control" id="technician_notes">
</div>
<div class="form-group">
<label for="admin_notes">Admin Notes</label>
<input type="text" name="admin_notes" class="form-control" id="admin_notes">
</div>
<div class="form-group">
<label for="invoice_status">Invoice Status</label>
<select name="invoice_status" id="invoice_status">
<option value="Client Not Yet Invoiced">Client Not Yet Invoiced</option>
<option value="Client Invoiced">Client Invoiced</option>
</select>
</div>
<div class="form-group">
<label for="invoice_number">Invoice Number</label>
<input type="text" name="invoice_number" class="form-control" id="invoice_number">
</div>
<input type="submit" id="btn_create" name="btn_create" class="btn btn-primary" value="Create Job Card">
</form>
My Form Action Page:
<?php
require_once "connection.php";
if(isset($_REQUEST['btn_create']))
{
$job_number = $_REQUEST['job_number'];
$date = $_REQUEST['date'];
$client_full_name = $_REQUEST['client_full_name'];
$client_email = $_REQUEST['client_email'];
$client_phone = $_REQUEST['client_phone'];
$item_for_repair = $_REQUEST['item_for_repair'];
$repair_description = $_REQUEST['repair_description'];
$hardware_details = $_REQUEST['hardware_details'];
$diagnostic_fee = $_REQUEST['diagnostic_fee'];
$tech_assigned = $_REQUEST['tech_assigned'];
$current_status = $_REQUEST['current_status'];
$technician_notes = $_REQUEST['technician_notes'];
$admin_notes = $_REQUEST['admin_notes'];
$invoice_status = $_REQUEST['invoice_status'];
$invoice_number = $_REQUEST['invoice_number'];
if(empty($date)){
$errorMsg="Please Enter date";
}
else if(empty($client_email)){
$errorMsg="Please Enter Email Address";
}
else
{
try
{
if(!isset($errorMsg))
{
$insert_stmt=$db->prepare('INSERT INTO repairs(job_number,date,client_full_name,client_email,client_phone,item_for_repair,repair_description,hardware_details,diagnostic_fee,tech_assigned,current_status,technician_notes,admin_notes,invoice_status,invoice_number) VALUES(:job_number,:date,:client_full_name,:client_email,:client_phone,:item_for_repair,:repair_description,:hardware_details,:diagnostic_fee,:tech_assigned,:current_status,:technician_notes,:admin_notes,:invoice_status,:invoice_number)');
$insert_stmt->bindParam(':job_number', $job_number);
$insert_stmt->bindParam(':date', $date);
$insert_stmt->bindParam(':client_full_name', $client_full_name);
$insert_stmt->bindParam(':client_email', $client_email);
$insert_stmt->bindParam(':client_phone', $client_phone);
$insert_stmt->bindParam(':item_for_repair', $item_for_repair);
$insert_stmt->bindParam(':repair_description',$repair_description);
$insert_stmt->bindParam(':hardware_details', $hardware_details);
$insert_stmt->bindParam(':diagnostic_fee', $diagnostic_fee);
$insert_stmt->bindParam(':tech_assigned', $tech_assigned);
$insert_stmt->bindParam(':current_status', $current_status);
$insert_stmt->bindParam(':technician_notes', $technician_notes);
$insert_stmt->bindParam(':admin_notes', $admin_notes);
$insert_stmt->bindParam(':invoice_status', $invoice_status);
$insert_stmt->bindParam(':invoice_number', $invoice_number);
if($insert_stmt->execute())
{
$insertMsg="Created Successfully........sending email now";
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<?php
if(isset($_POST['btn_create'])){
$to = "EMAIL_ADDRESS"; // this is your Email address
$from = "EMAIL_ADDRESS"; // this is the sender's Email address
$job_number = $_POST['job_number'];
$date = $_POST['date'];
$client_full_name = $_POST['client_full_name'];
$item_for_repair = $_POST['item_for_repair'];
$subject = "JC$job_number has been added to ECEMS";
$message = "Hi Admin. A new job card has been added to ECEMS on the $date for $client_full_name. The item for repair is a $item_for_repair. Please start diagnostics immediately for this item.";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
header("refresh:1;repairs.php");
}
?>
I tried to follow this tut: Send email with PHP from html form on submit with the same script
I have also tried activating errors on this page with no results:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
I am new and have NEVER done a code like this where the AUTO INCREMENT number needs to be sent in an email. Please can someone assist me. I can edit my question if more clarification is needed.
EDIT: Ive done some research and found i can use the lastInsertID (https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id function. Some help implementing this would be so appreciated.
$insertId = false;
if($insert_stmt->execute())
{
$insertId = $insert_stmt->insert_id;
$insertMsg="Created Successfully........sending email now";
}
if($insertId){
// do stuff with the insert id
}
Ok so i used the MAX method to solve this issue. i added the following to the top of my createnewrepairs.php page:
$stmt = $db->prepare("SELECT MAX(job_number) AS max_id FROM repairs"); $stmt -> execute(); $job_number = $stmt -> fetch(PDO::FETCH_ASSOC); $max_id = $job_number['max_id'];
Then on the same page, i added the following form field
<div class="form-group">
<label for="job_number">Job Number</label>
<input type="job_number" name="job_number" id="job_number" class="form-control" value="<?php echo $max_id+1;?>" readonly>
</div>
Then on the form processing page (processnewrepair.php) my code in my original post worked and generated the AUTO INCREMENT NUMBER to send in an email.
I have a problem to create the select box then get the value to show in the other input field.
Below is what I have tried the code:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="<?php echo $function_code_select;?>">
</div>
</div>
I have define $function_code_select = $rs_incharge['function_code']; because I need to get the function code number to show in the second input field, so that the second input field I have written echo $function_code_select; to show the value, but it cannot work. Hope someone can guide me how to solve it. Thanks.
Below is the sample what I need to show the output. If work it can show me correct the function_code in the second input field:
You can't do that with PHP, it's a server-side script lang.
Add some javascript to make the magic happen:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="New Category_value">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="<?php echo $function_code_select;?>">
</div>
</div>
<script>
function getComboA(selectObject) {
var value = selectObject.value;
document.getElementById("function_code").value =value;
}
</script>
If you need to keep value="", you can add an extra attribute like data-function-code=""
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select onchange="getComboA()"
class="form-control blank"
id="parentid"
name="parentid"
title="parentid">
<option>Please Select</option>
<option value="New Category_value"
data-function-code="' . $rs_incharge['function_code'] . '">
New Category
</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="cp1"
class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text"
class="form-control"
id="function_code"
name="function_code"
title="function_code"
value="<?php echo $function_code_select;?>">
</div>
</div>
<script>
function getComboA() {
var sel = document.getElementById('parentid');
var selected = sel.options[sel.selectedIndex];
var data = selected.getAttribute('data-function-code');
document.getElementById("function_code").value =data;
}
</script>
I have a problem to set the input box become disable with condition. My condition is if the value is 0, then the input box will become disable. I have add readonly in the html box, but it also can't work.
Below is what I am tried the coding, Hope anyone can guide me to check which part I am getting wrong.:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<?php
if($rs_incharge['id'] != '0'){
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
}else($rs_incharge['id'] = '0'){
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code' readonly>
}
?>
</div>
</div>
Actually I want the output like below the picture:
Your code contains many errors.
And your question is not clear.
If you want to make input boxes as readonly when you select 'New Category' ( value is 0), and otherwise make input boxes editable, you need to use javascript or jQuery.
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Name:</label>
<div class="col-lg-3">
<input type='text' class='form-control' id='activity_name' name='activity_name' title='activity_name'>
</div>
</div>
jQuery code
$(document).ready(function(){
$('#parentid').change(function(){
if ( $(this).val() == '0' ){
$('#activity_code, #activity_name').attr('readonly', 'readonly');
}else{
$('#activity_code, #activity_name').removeAttr('readonly');
}
});
});
I am trying to work out how to add multiple rows in an 'Order Details' table when a form is submitted. The user is able to select multiple sizes which when submitted, should generate up to N number of rows selected by the user.
Below is my current code for the View file:
<form action="../Controller/NewProduct.php" method="POST">
<h4 align="center"> PRODUCT <b>DETAILS</b> </h4>
<br/>
<div class="form-group">
<label for="categoryid">Category</label>
<select class="form-control" name="categoryid" id="categoryid" required>
<option value="1">Tshirt</option>
<option value="2">Swim Shorts</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="productid">Product ID</label>
<input name="productid" class="form-control" id="productid" required>
</div>
<br/>
<div class="form-group">
<label for="name">Product Name</label>
<input name="name" class="form-control" id="name" required>
</div>
<br/>
<div class="form-group">
<label for="description">Product Description</label><br/>
<textarea name="description" rows="4" class="description" placeholder="Enter product description here." required></textarea>
</div>
<br/>
<div class="form-group">
<label for="size">Size</label>
<select class="form-control" id="size" name="size" multiple required>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="XXL">XXL</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="colour">Colour</label>
<select class="form-control" id="colour" name="colour" required>
<option value="Burgundy">Burgundy</option>
<option value="Navy Blue">Navy Blue</option>
<option value="Red">Red</option>
<option value="White">White</option>
<option value="Grey">Grey</option>
<option value="Black">Black</option>
<option value="Yellow">Yellow</option>
<option value="Green">Green</option>
<option value="Khaki">Khaki</option>
<option value="Blue">Blue</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="unitprice">Unit Price</label>
<input name="unitprice" class="form-control" id="unitprice" placeholder="Do not include the £ sign." required>
</div>
<br/>
<div class="form-group">
<label for="unitsinstock">Units In Stock</label>
<input name="unitsinstock" class="form-control" id="unitsinstock" required>
</div>
<br/>
<div class="form-group">
<label for="pimage">Product Image</label>
<input name="pimage" class="form-control" id="pimage" placeholder="Enter Image URL" required>
</div>
<br/>
<div class="form-group">
<label for="discount">Discount</label>
<input name="discount" class="form-control" id="discount">
</div>
<br/>
<button type="submit" name="submit" class="btn"><i class="fa fa-check"></i> Done</button>
</form>
This this then the file which is run when the form is submitted:
if (isset($_POST['submit'])) {
require_once 'DatabaseConnection.php';
$statement = $pdo->prepare("INSERT INTO Product (categoryid, name, description, colour, unitprice, pimage, discount, productid)
VALUES (:categoryid, :name, :description, :colour, :unitprice, :pimage, :discount, :productid)");
$statement->bindParam(':categoryid', $categoryid);
$statement->bindParam(':name', $name);
$statement->bindParam(':description', $description);
$statement->bindParam(':colour', $colour);
$statement->bindParam(':unitprice', $unitprice);
$statement->bindParam(':pimage', $pimage);
$statement->bindParam(':discount', $discount);
$statement->bindParam(':productid', $productid);
$categoryid = $_POST['categoryid'];
$name = $_POST['name'];
$description = $_POST['description'];
$colour = $_POST['colour'];
$unitprice = $_POST['unitprice'];
$pimage = $_POST['pimage'];
$discount = $_POST['discount'];
$statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
$statement2->bindParam(':size', $size);
$statement2->bindParam(':unitsinstock', $unitsinstock);
$statement2->bindParam(':productid', $productid);
$size = $_POST['size'];
$unitsinstock = $_POST['unitsinstock'];
$productid = $_POST['productid'];
$statement->execute();
$statement2->execute();
header('location:../View/AdminAllProducts.php');
}
?>
An example of what I am trying to achieve is like the following: https://imgur.com/a/SPr14
Pick up the sizes array and work on it in a loop:
$sizes = $_POST['size[]'];
foreach ( $sizes as $size )
{
// ... your code of statement2 :
statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
$statement2->bindParam(':size', $size);
$statement2->bindParam(':unitsinstock', $unitsinstock);
$statement2->bindParam(':productid', $productid);
$unitsinstock = $_POST['unitsinstock'];
$productid = $_POST['productid'];
$statement->execute();
$statement2->execute();
}
You should change the selects that can be multiple select to be an array (ex: size([]) the in PHP you should work the request value as an array.
You can also concatenat the string in the values to be inserted in order to only do one request to the database.
I have one dynamic dropdown list where i get ID and Name.
And i have 2 input box above this.
If user will select any value from dropdown then its ID and Name should be display in input boxes.
How to get this in jquery ?
Below is my code for getting dropdown list from database.
<div class="col-md-12">
<?php
$m_option_selectedvalue = !empty($m_option)?$m_option:"";
$m_option_selectbox = '<select " name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>';
foreach ($main_menu_list as $row):
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
$m_option_selected = ($m_option_selectedvalue == $m_option_value)?" selected ":"";
$m_option_selectbox = $m_option_selectbox .'<option value="'.$m_option_value.'" '.$m_option_selected.'>'.$m_optione_desc.'</option>';
endforeach;
$m_option_selectbox .= "</select>";
?>
</div>
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
I am getting id from $m_option_value this variable.
And name from $m_optione_desc this variable.
Below is my code where i want this 2 values:
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>
try this
$('#id_menu').change(function(){
var opt = $(this).find('option:selected');
$('#role').val(opt.val());
$('#role_name').val(opt.html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>
<option value="1"> item 1</option>
<option value="2"> item 2</option>
<option value="3"> item 3</option>
</select>
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>