I am creating a form for inserting data which technologies are being used by which customers.
I get data of customer's ID and name and select a customer in an drop-down list, and i get data from technologies id and description and display description in a table + it creates a textbox (ID='box-". $row1['ID_T']."') for every technology entry in a DB.
So now i would like to check this dynamically created textboxes with jquery for value (if empty or filled with data) (getelementbyid) but i can not find a way to check theese DYN textboxes.
The ID_T and ID_C will be loaded into another table containing these two PK's and add string from textbox to value.
i would appreciate your help so much!
<HTML>
<HEAD>
<SCRIPT>
function update_tech(id,description)
{
var x = confirm('Do you want to edit technology '+description);
if (x == true)
{
document.getElementById('update_id').value = id;
//document.getElementById('description').value = description;
//document.form_update.submit();
}
}
</SCRIPT>
</HEAD>
<?php
include "connection.php";
$sql = "SELECT ID_C, Name FROM empresa";
$rs = mysql_query($sql, $conn);
$sql2 = "SELECT ID_T, description FROM technologies";
$rs2 = mysql_query($sql2, $conn);
while($row = mysql_fetch_array($rs2))
{
if (isset($_POST["box-".$row["ID_T"]]))
if ($_POST["box-".$row["ID_T"]] != "")
echo "INSERT ....".$_POST["box-".$row["ID_T"]]."<br>";
}
$rs2 = mysql_query($sql2, $conn);
mysql_close($conn);
?>
<BODY>
<SELECT NAME="customers" ONCHANGE="showCustomer(this.value)">
<?php
if (mysql_num_rows($rs))
{
while($row = mysql_fetch_array($rs))
{
?>
<option value='<?php echo $row['ID_C'] ?>'><?php echo $row['Name']?></option>
<?php
}
}
else {
echo "<option value=\"0\">No customers</option>";
}
?>
</SELECT>
<FORM METHOD="POST">
<?php
echo "<table border='0'>";
while($row1 = mysql_fetch_array($rs2))
{
echo "<tr>";
echo "<td><INPUT TYPE='text' name='box-". $row1['ID_T']."' ID='box-". $row1['ID_T']."'></td>";
echo "<td>" . $row1['description'] . "</td>";
echo "</tr>";
}
echo "</TABLE>";
?>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
You can either maintain an array of input box ids or use the jquery partial selector to identify all the input boxes.
jQuery approach is something like:
$('input[id^="box-"]').each(function(e, i) { console.log(e); };
Related
This code below is displaying data through a dropdown and I was wondering on how can I convert this into like a when a user enters a number in a textbox and click set, dynamic dropdowns appear based on the number he entered.
$query = mysqli_query($con, "SELECT * FROM topic WHERE SubCat = $subcat ");
echo "<select name='topdd' >";
echo " <option>--None Selected--</option>";
while ($row = mysqli_fetch_array($query)) {
echo "<option value='$row[topic_id]' selected>";
echo $row["title"];
echo "</option>";
}
echo "</select>";
This code it's ok for you ? I make your job, the next time try yourself
<form method="POST">
<label for='choiceNumber'>Number of dropdowns choice</label>
<input type='number' name='choiceNumber'>
<input type='submit'>
</form>
<?php
$resultArray = array(1,2,3,4,5,6,7);
$display = "<select name='topdd'><option>--None Selected--</option>";
if (!empty($_POST['choiceNumber']) && $_POST['choiceNumber'] > 0) {
for($i = 0; $i < $_POST['choiceNumber']; $i++) {
$display .= "<option>".$resultArray[$i]."</option>";
}
}
echo $display."</select>";
?>
I am stuck on a code where I want to fetch data from MySQL into an array.. I have a form containing color and size select boxes, an onclick javascript function is triggered and it created two more select boxes like above, I have managed to get data into the javascript code where the code for creating new select boxes is written.
But I am only getting the last inserted record from both the tables. Although I have used a while loop.
Can someone help me out and I am also getting name of all select boxes likes name="color[]" , I want to insert records into a bridge table containing ids of color and size. below is my code please help ..
I will clear it up , each time I click add more button it should create 2 new dropdown lists, one for color and 2nd for size, both dropdowns should have the distinct data from database. so the ids for each record would be same in every dropdown list, I want to add more than 1 records in bridge table which contains product_id,color_id and size_id, so if I go for 3 dropwdown boxes , and i select blue color and small size in the first, then for second dropdown i again select blue color and size medium, as for the last dropdown which was also generated by the javascript function . i choose black color and large size. so from the dropdown it will get ids of size,color and it would be inserted accordingly.. so when i display the product and color blue is selected i would only see the sizes which were added to the color blue at the time of adding the product.. i hope this clears everything :)
$result=mysql_query("SELECT * FROM color,size");
while($row=mysql_fetch_array($result)) {
?>
<script>
var room = 1;
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds')
var divtest = document.createElement("div");
divtest.innerHTML = '<div class="label">Room ' + room + ':</div><div class="content"><span>Color: <select name="color[]"><option value="<?php echo $row['color_id']; ?>"><?php echo $row['color']; ?></option></select></span><span>Size: <select><option value="<?php echo $row['size_id']; ?>"><?php echo $row['size']; ?></option></select></span></div>';
objTo.appendChild(divtest)
}
</script>
<?php
}
HTML code
<div id="room_fileds">
<div>
<div class='label'></div>
<div class="content">
<input type="button" class="btn btn-success" id="more_fields" onclick="add_fields();" value="Add More" /> <br /><br />
<select name="color[]" class="form-control">
<option value="0">Select Color</option>
<?php
$result=mysql_query("SELECT * FROM color");
while($row=mysql_fetch_array($result)){
?>
<option value="<?php echo $row['color_id'] ?>"><?php echo $row['color']; ?></option>
<?php } ?>
</select>
<select name="size[]" class="form-control">
<option value="0">Select Size</option>
<?php
$result=mysql_query("SELECT * FROM size");
while($row=mysql_fetch_array($result)){
?>
<option value="<?php echo $row['size_id'] ?>"><?php echo $row['size']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
Replace your first code with the following:
<script>
var colors = [];
var sizes = [];
var room = 1;
<?php
$result = mysql_query("SELECT * FROM color");
while ($row = mysql_fetch_array($result)) { ?>
colors.push(['<?php echo $row['color_id'] ?>', '<?php echo $row['color'] ?>']);
<?php }
$result = mysql_query("SELECT * FROM size");
while ($row = mysql_fetch_array($result)) { ?>
sizes.push(['<?php echo $row['size_id'] ?>', '<?php echo $row['size'] ?>']);
<?php } ?>
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds');
var divtest = document.createElement("div");
var html = '<div class="label">Room ' + room + ':</div><div class="content"><span>Color: <select name="color[]" class="form-control">';
for (i = 0; i < colors.length; i++) {
html += '<option value="' + colors[i][0] + '">' + colors[i][1] + '</option>';
}
html += '</select></span><span>Size: <select name="size[]" class="form-control">';
for (i = 0; i < sizes.length; i++) {
html += '<option value="' + sizes[i][0] + '">' + sizes[i][1] + '</option>';
}
html += '</select></span></div>';
divtest.innerHTML = html;
objTo.appendChild(divtest);
room++;
}
</script>
$divs='';
$result=mysql_query("SELECT * FROM color,size");
$room=1;
while($row=mysql_fetch_array($result)) {
$divs.='<div><div class="label">Room ' . $room . ':</div><div class="content"><span>Color: <select name="color[]"><option value="'.$row['color_id'].'">'.$row['color'].'</option></select></span><span>Size: <select><option value="'.$row['size_id'].'">'.$row['size'].'</option></select></span></div></div>';
$room++;
} ?>
<script>
function add_fields() {
var objTo = document.getElementById('room_fileds');
objTo.appendChild(<?php echo $divs; ?>);
}
</script>
Try this code hope it works for you
<script>
var room = 1;
function add_fields() {
room++;
var objTo = document.getElementById('room_fileds');
var divtest = document.createElement("div");
<?php
$div_start='<div class="label">Room '."'+ room +'".' :</div><div class="content">';
$color='<span>Color: <select name="color[]">';
$size='<span>Size: <select>'
$result=mysql_query("SELECT * FROM color,size");
while($row=mysql_fetch_array($result)) {
$color.='<option value="'.$row['color_id'].'">'.$row['color'].'</option>';
$size.='<option value="'.$row['size_id'].'">'.$row['size']'.</option>';
}
$color.='</select> </span>';
$size.='</select> </span>';
$div_close='</div>';
$innerHTML=$div_start.$color.$size.$div_close;
?>
divtest.innerHTML ='<?php echo $innerHTML; ?>';
objTo.appendChild(divtest);
}
</script>
This is the final code , it would help if anyone wants to code a similar thing.. just change values and it would work as per your need... and i would like to thank all people who replied to the thread and specially Mohammad Anini .. it wouldnt have been possible without his help !!!
$query = mysql_query("INSERT INTO products (product_name,product_description, product_pic1, product_pic2, product_pic3,product_price,category_id,subcategory_id,product_status,color,size,product_slug, meta_keywords,entrydate)
VALUES ('$product', '$description' , '$image', '$image2', '$image3', '$product_price', '$category', '$subcategory', '$product_status', '$capture_field_vals', '$size', '$product_slug1', '$meta_keywords', Now() )") or die(mysql_error());
if ($query === TRUE) {
$lastid = mysql_insert_id();
$color["color"]=array();
$size["size"]=array();
foreach ($_POST['color'] as $key => $colorvalue) {
}
foreach ($_POST['size'] as $key => $sizevalue) {
}
$cid=array();
foreach($color as $rec){
$cid[]=$rec;
}
$sz=array();
foreach($size as $rec){
$sz[]=$rec;
}
$length=sizeof($color);
for($i=0;$i<$length-1;$i++){
$query2 = mysql_query("INSERT INTO bridge (product_id,color_id, size_id)
VALUES ('$lastid', '$cid[$i]' , '$sz[$i]')") or die(mysql_error());
// echo "<script>alert('New Product successfully Addedd');windows.location.replace('addedit_product.php');</script>";
}
I am trying to make a dropdown menu from a database. I am returning nothing right now. For the menu, I need to choose a object then run a SQL query on it. This query will populate a table and will be dynamic.
Here is the code, please help.
<html>
<?php
include('header.php');
?>
<h1>Chemicals Search</h1>
<br/ >
<br/ >
</head>
<h1>
<center>Chemical Search</center>
</h1>
<form action="chemicals.php" method="post">
<label>Search By Product:</label>
<?php
//making the chemical array
$con = mysqli_connect("...");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$p = mysqli_query($con, "SELECT product_name FROM product");
$products = mysqli_fetch_array($p);
echo '<select name="product">';
foreach ($product as $key => $value) {
echo "<option value=\"$key\"> $value</option>\n";
}
echo '</select>';
echo "<br>";
?>
</form>
Your Query only selects product name make a little changes to it
$p = mysqli_query($con,"SELECT product_id,product_name FROM product");
echo'<select name="product">';
if(mysqli_num_rows($p) > 0)
{
while($products =mysqli_fetch_array($p))
{
echo"<option value='".$products['product_id']."'>".$products['product_name']."</option>\n";
}
}
echo '</select>';
echo"<br>";
I am sure this will works, works for me everytime
I have searched endlessly for an answer but have found none. I am trying to get the id of a clicked item. The item that I am clicking is from mysql database and has been displayed through a for loop. When the item is clicked I am taken to another page. In this page I want to utilize the id from the clicked item to get other information from that row in mysql database; this much I can do. The problem is getting the id from the clicked item and sending it.
This is the most recent way that I tried:
First I displayed the items from mysql.
<?php
$query = "SELECT `video` FROM `challenge_name` ORDER BY `id`";
$result = mysql_query($query);
if($result = mysql_query($query))
{
for($i = 0; $i < mysql_num_rows($result); $i++)
{
$id = $i;
$code = "<div id=\"challenge_preview\"><h7 class=\"challenge_preview_item\"
id=\"challenge_preview_name\"></h7><a href=\"challengeprofile.php\">
<video
class=\"challenge_preview_item\" id=\"challenge_video\"
src=\"".mysql_result($result, $i)."\"></video></a></div>";
echo $code;
}
}
else
{
die('Couldn\'t connect'. mysql_error());
}
?>
Than I put the id in a hidden form so that I could attempt to POST it to the other page:
<form action="<?php echo $current_file; ?>" method="POST">
<input type="hidden" name="id" value="14">
</form>
On the script.php file that is included in both pages, I put
$(#challenge_video).click(function(){ <?php $id = $_POST['id']; ?> ;});
And on the page that the id is being posted to I put
<?php
include 'script.php';
echo getChallengeData('name', 'id', $id)
?>
Please help, Thank you
These are the edits
<div id='challenge_previews'>
<?php
$query = "SELECT `video` FROM `challenge_name` ORDER BY `id`";
$result = mysql_query($query);
if($result = mysql_query($query))
{
for($i = 0; $i < mysql_num_rows($result); $i++)
{
$id = $i;
echo "<div id=\"challenge_preview\"><video class=\"challenge_preview_item challenge_video\" src=\"".mysql_result($result, $i)."\"></video></div>";
}
}
else
{
die('Couldn\'t connect'. mysql_error());
}
?>
<form action="<?php echo $current_file; ?>" method="GET">
<input type="hidden" name="id" value="14">
</form>
</div>
This is the code for page 2
<h1 id="challenge_profile_name">
<?php
$id = substr(base64_decode($_GET['id']),6);
echo getChallengeData('name', 'id', $id);
?>
</h1>
This is the code for the getChallengeData method in the core.inc.php
function getChallengeData($field1, $field2, $field3)
{
$query = "SELECT `$field1` FROM `challenge_name` WHERE `$field2` = '$field3'";
if($query_run = mysql_query($query))
{
if($query_result = mysql_result($query_run, 0, $field1))
{
return $query_result;
}
}
}
That error that I'm getting has to do with the implementation of the getChallengeData method on page 2. The error says
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 10 in C:\xampp\htdocs\ChallengeNetworkWebsite\core.inc.php on line 42
How can I collect the data on the row from the table that I select and use it in the result?
Here is the javascript I am using to show the data entry screen, once the function has been called by selecting the row. Now I just need to design a form in PHP that will include (1) some of the data from the row selected and (2) some new data that will be collected.
Here is the Javascript to select the row and call the data entry form
$(document).ready(function () {
$("tr").live('click',function(){
$.post('data_entry_form.php', function(data) {
$('#section2').html(data);
});
});
});
Here is the PHP Script
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
The "Correct" answer to this problem is NOT to read from the DOM. Never a good idea. I suggest you pass the record id to the ajax call and have the ajax call return an already-populated form.
//PHP
//place the data attribute in the tr
echo "<tr data-recordId='".$row['id']."'>";
//JS
$(document).ready(function () {
$("tr").live('click',function(){
//Get the ID from the row clicked
var id = $(this).data('recordId');
//short-hand
$('#section2').load('data_entry_form.php?id='+id);
});
});
Then your ajax page would just read $_REQUEST['id'] to get the id of the form being edited.
//Ajax side PHP
$id = (int)$_REQUEST['id'];
//Fetch data and use it to pre-populate form item
You would pre-populate your inputs like this
<input type="text" value="<?php echo $row['value']; ?>" />
or
echo '<input type="text" value="'.$row['value'].'" />';
NOTE: If your values contain quotes, you will want to replace them with the code "
echo '<input type="text" value="'.str_replace('"', '"', $row['value']).'" />';
Within the event handler, this and $(this) refer to your selected row:
$("tr").live('click',function(){
// this.cells[1] is the cell that contains Pubco Name
// You can also use $(this).children("td")[1]
...
});