I am doing a registration page where the one input textbox should be populated based on 2 other input textboxes.
tblEntries
Id; firstname; lastname; age; gender; categories
tblCategories
Id; categories; age_from; age_to, gender
Data inserted: 1; Veteran_male; 20; 30; male
frmEntries.php is populated with tblEntries data onload already using localstorage.
(may look like this
Firstname=Peter
Lastname=Trump
Age=25
Gender=male
Categories= ?(from sql)
include("conn.php");
include("categories.php");
<input id="firstname" type="text" name="firstname" value="" />
<input id="lastname" type="text" name="lastname" value="" />
<input id="age" type="text" name="age" value="" />
<input id="gender" type="text" name="gender" value="" />
<input id="categories" type="text" name="categories" value="<?php echo $output; ?>" />
(the 25 fall in between 20 and 30 in the tblCategories and the gender is shown in both tables)
categories.php
<php?
include ('conn.php');
$gender = $_POST["gender"];
$age = $_POST["age"];
$result = $con->query("SELECT categories FROM tblCategories WHERE '$age' BETWEEN age_from AND age_to AND gender='$gender'”);
$result = mysqli_query($con, $query);
$output = '';
while($row = mysqli_fetch_array($result))
{
$output = $row["categories"];
}
echo($output);- //(to be populated inside the frmEntries and the
“categories” field onload)
}
?>
Should i use Ajax with a button?
<script>
$('#updateBtn').click(function(e){
//to disable the click from going to next page
e.preventDefault();
$.ajax(
'categories.php', function(data){
$('#categories').val(data);
}
);
});
</script>
Please advise on how to do this. Should I rather use a button to populate "categories" after onload? I get variable not find errors
Related
Wondering if someone can point me in the right direction.
This is the main page of the project (index.php). The script below is receiving values from fetch.php which displays properly. All I need is to autocomplete the 2 input boxes client_id and title. I know the problem is with the script, but can't figure it out.
<input type="text" id="client_id" name="client_id" placeholder="ID" />
<input type="text" id="status" name="status" placeholder="Status" autocomplete="off" />
<input type="text" id="title" name="title" placeholder="Client" autocomplete="off" />
function setText(obj){
var val = obj.value;
console.log(val);
function concat(){
var x = <?php echo $client_id; ?>
var y = <?php echo $first_name; ?>
document.getElementById("client_id").value = x;
document.getElementById("title").value = y;
}
concat();
}
If was was to have the following script, title would fill in automagically.
function setText(obj){
var val = obj.value;
console.log(val);
document.getElementById('title').value = val;
}
You're mixing php in with javascript in a way that doesn't make sense, since you say the values are coming in from a fetch
If it really is PHP, then (obviously) you could set the inputs directly
<input type="text" id="client_id" name="client_id" placeholder="ID" value="<?php echo $client_id; ?>" />
<input type="text" id="status" name="status" placeholder="Status" autocomplete="off" />
<input type="text" id="title" name="title" placeholder="Client" autocomplete="off" value=" <?php echo $first_name; ?>" />
If it's coming in through fetch, you could do it this way:
let ref = 12; // the reference to get the data
fetch(`http://example.com/getClientData.php?ref=${ref}`)
.then(response => response.json())
.then(data => {
document.getElementById("client_id").value = data.client_id;
document.getElementById("title").value = data.client_title;
console.log(data)
});
I have a form that had text and dropdowns. Now the client wants to use checkbox instead of dropdown. How to I change the query . Here is my code:
Form is in join.php
<form name="pantryinfo" id="pantryForm" method = "post" action="email.php" data-toggle="validator" role="form">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Pantry Name" required>
<label>Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter Street address" >
<p class="help-block"></p>
<input type="checkbox" id="snapeligible" name="snapeligible" value="Yes">SNAP Eligilble<br>
<label>Community Meal?
select class="form-control" id="communitymeal" name="communitymeal" required >
<option></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</label>
<input name="submit" type="submit" id="submit" value="Add Food Provider" class="btn btn-primary center-block">
</form>
The field SNAP Eligilble was a dropdown just like Community Meal but now is a check box.
My earlier query was as follows in email.php
require 'config/connectDB.php';
//set variables from the form
$pname = $_POST['name'];
$paddress = $_POST['address'];
$psnapeligible = $_POST['snapeligible'];
$pcommunitymeal = $_POST['communitymeal'];
$sql = "INSERT INTO temp(pan_id, pname, paddress, psanpeligible, pcommunitymeal, )
VALUES(NULL,'$pname', '$paddress', '$psnapeligible', '$pcommunitymeal',)";
I am not sure how to append the query to include data from the check box only when the checkbox is selected as its not a required field.
An unchecked checkbox will not even be found in the $_POST array. This means to see from the PHP side whether the box was checked, all you have to do is look at whether the matching variable is set.
Change:
$psnapeligible = $_POST['snapeligible'];
To:
$psnapeligible = isset($_POST['snapeligible']) ? 'yes' : 'ho';
Make an IF-Statement, where you proof if the Value of the Checkbox is set.
Is it set use this Statement
Else use the another Statement without the psnapeligible
if (isset ($psnapeligible)) {
$sql = "...";
else {
$sql = "...";
}
you can chek in your php page (email.php) if your chek box is set or not ...
if (isset($_POST['snapeligible'])) {
$psnapeligible = $_POST['snapeligible'];
}
else {
$psnapeligible = "No" ;
}
I'm working on small project for data entry.
let assume a table named material fields
id , name , visible , position
and other table named received_material fields
id , material_id , quantity , supplier
and one simple html form for receiving materials, structure here
<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
<label for="date">date</label>
<input type="text" name="date" />
<label for="name">Material Name</label>
<input type="text" id="name" name="name" />
<label for="quantity">Weight</label>
<input type="text" name="quantity" />
<label for="supplier">Supplier</label>
<input type="text" name="supplier" />
<input type="submil" value="submit"/>
</form>
<script>
$('#name').autocomplete({source : 'supplier_chk.php'});
</script>
the supplier_check.php Script here...
<?php include 'includes/session.php'; ?>
<?php include 'includes/functions.php'; ?>
<?php if(!logged_in()) {
redirect_to('login.php');
}
?>
<?php if ( logged_in_role('administrator') || logged_in_role('store') ) { ?>
<?php echo msg(); ?>
<?php
$search = escape_string($_GET['term']);
$sql = "SELECT id, supplier_name FROM `supplier`
WHERE `supplier_name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);
$json = [];
while ( $row = mysqli_fetch_assoc($result) )
{
$json[] = $row['supplier_name'];
}
echo json_encode($json);
}
?>
I'm using JQuery UI For Name Autocomplete. fetching material name throw ajax method. it'll easily autocomplete material name but i've to store material id instead material name.. please help how to do this action using jquery/javascript or php.
add hidden field before name input give id to get element..
here html example
<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
<label for="date">date</label>
<input type="text" name="date" />
<label for="name">Material Name</label>
<input type="hidden" id="id" name="id" />
<input type="text" id="name" name="name" />
<label for="quantity">Weight</label>
<input type="text" name="quantity" />
<label for="supplier">Supplier</label>
<input type="text" name="supplier" />
<input type="submil" value="submit"/>
</form>
<script>
$('#name').autocomplete({
minLength: 0,
source : 'material_chk.php',
focus: function( event, ui ) {
$( "#name" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#name" ).val( ui.item.label );
$( "#id" ).val( ui.item.id );
return false;
}
});
</script>
in material_chk.php
<?php
$search = escape_string($_GET['term']);
$size = isset($_GET['size'])? escape_string($_GET['size']): null;
$sql = "SELECT * FROM `materials`
WHERE `name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);
$json = [];
while ( $row = mysqli_fetch_assoc($result) )
{
$json[] = array("id"=>$row['id']),"label"=>$row['item_name']);
}
echo json_encode($json);
}
I'm note sure what you're asking, but I do have a tip. This is a general tip, not just restricted to suppliers, but I will use them as an example.
I would use a better solution: a dropdown list with all the supplier names. The value of each list item is the supplier ID. So you directly submit the supplier ID.
<select id="suppliers" name="supplierSelector">
<option value="1">Philips</option>
<option value="2">Sony</option>
<option value="3">Samsung</option>
<option value="4">Akai</option>
</select>
You could additionally put a search text input below the dropdown list. Anything typed into that search box will restrict the dropdown list. This can be done in Javascript.
But basically my solution is to let the user select the correct supplier ID using their names. in PHP that would be:
$sql = 'SELECT id, supplier_name FROM supplier';
$result = mysqli_query($connection,$sql);
echo '<select id="suppliers" name="supplierSelector">'.PHP_EOL;
while ($supplier = mysqli_fetch_assoc($result))
{
extract($supplier);
echo '<option value="'.$id.'">'.$supplier_name.'</option>'.PHP_EOL;
}
echo '</select>'.PHP_EOL;
The added advantage of this method is that the form cannot be submitted without a valid supplier. In general you should try to restrict the input to what you know is allowed.
I have a table callled tb_app with fields 'id','name','aic','batchcode'
example: field values '1','james','0001','1'
If type james in the textbox(name) corresponding values for other fields(aic,batchcode) should be displayed in textboxes. Is this possible? can anyone help me...I'm a php beginner ...Please!
Form code:
<form method="post">
<input type="text" name="names" id="query" />
<input type="text" name="aic" />
<input type="text" name="batchcode" />
<input type="submit" name="show" />
</form>
You can do this by Jquery ajax Try in this way by using your exact field names it might help you
Your jquery script should be something like this
function getvalues()
{
var selname = $("input[name='names']:text"").val();
$.ajax({ url: "getuserdata.php",
data: {"selname":selname},
type: 'post',
success: function(output) {
$("#aic").val(output);
$("#batchcode").val(output);
}
});
}
and your Html code
<input type="text" name="names" id="query" onblur="getvalues()"/>
<input type="text" name="aic" id="aic"/>
<input type="text" name="batchcode" id="batchcode" />
The getuserdata.php
<?php
include("database connection file here");
$selname = $_POST['selname'];
$query = "SELECT * FROM table_name WHERE youruser_id = $selname";
$res = mysql_query($query);
$rows = mysql_fetch_array($res);
echo $rows['aic'];
echo $rows['batchcode'];
?>
I have a sort of complex php,html script that I am using to create an interactive design. I am using a repeat table and i am basically cloning the contents of a particular table row and appending it to the end of the table. When I select a certain option in the select menu, the corresponding textfields get updated with the correct values. However cloned rows misbehave
What I'm asking is if there is a way to make this fields also change values. Here is the various code
Code for adding/cloning a row. Please note the id details is the id of a table row
$("#addrow1").click(function(){
//alert("It works");
//$('#description tr:last').after('<tr>...</tr>');
$('#details').clone().appendTo('#dailyexpense');
});
Code for changing the textfield and textbox values
$("#cities").live('change',function(){
var cityrates = $("#cities :selected").val();
var brkfstrates = (0.2 * cityrates).toFixed(1);
$("#brkfasttxt").val(brkfstrates);
$("#brkfastchk").val(brkfstrates);
var lunchrates = (0.3 * cityrates).toFixed(1);
$("#lunchtxt").val(lunchrates);
$("#lunchchk").val(lunchrates);
var dinnerrates = (0.3 * cityrates).toFixed(1);
$("#dinnertxt").val(dinnerrates);
$("#dinnerchk").val(dinnerrates);
var incdntlrates = (0.2 * cityrates).toFixed(1);
$("#incidentltxt").val(incdntlrates);
$("#incidentlchk").val(incdntlrates);
});
My table row code for the one that gets loaded by the browser
<tr id="details">
<td><label for="date"></label>
<input style="background-color:#CCC;" type="text" name="date" id="date" /></td>
<td><label for="cities"></label>
<select name="cities" id="cities" style="background-color:#CCC; width:220px;">
<?php
foreach($country as $makassi1){
$selectcities = "SELECT City, Country, Rate FROM perdiem_rates WHERE Country = '$makassi1'";
$checkcity = mysql_query($selectcities)or die(mysql_error());
$countcities = mysql_num_rows($checkcity);
while($row=mysql_fetch_assoc($checkcity))
{
$countries = ($row['Country']);
$names =($row['City']) ;
$rate =($row['Rate']) ;
$ratess=$countries."-".$names
?>
<option id="cityoptrates" value="<?php echo $rate; ?>">
<?php echo $ratess; ?>
</option>
<?php
}
}
?>
</select></td>
<td><input name="brkfastchk" type="checkbox" class="chkbox" id="brkfastchk" />
<label for="brkfasttxt"></label>
<input style="background-color:#CCC;" name="brkfasttxt" type="text" id="brkfasttxt" size="5" readonly="readonly" />
<label for="brkfastchk"></label></td>
<td><input type="checkbox" name="lunchchk" id="lunchchk" class="chkbox" />
<label for="lunchtxt"></label>
<input style="background-color:#CCC;" name="lunchtxt" type="text" id="lunchtxt" size="5" readonly="readonly" />
<label for="lunchchk"></label></td>
<td><input type="checkbox" name="dinnerchk" id="dinnerchk" class="chkbox" />
<label for="dinnertxt"></label>
<input style="background-color:#CCC;" name="dinnertxt" type="text" id="dinnertxt" size="5" readonly="readonly" />
<label for="dinnerchk"></label></td>
<td><input type="checkbox" name="incidentlchk" id="incidentlchk" class="chkbox" />
<label for="incidentltxt"></label>
<input style="background-color:#CCC;" name="incidentltxt" type="text" id="incidentltxt" size="5" readonly="readonly" />
<label for="incdntchk"></label></td>
<td colspan="2"><label for="daily_totals"></label>
<input style="background-color:#CCC;" name="daily_totals" type="text" id="daily_totals" size="5" /></td>
</tr>
As per the above my textfield value changing code works perfectly with the first initial row. The cloned rows don't work. The only one which works on the cloned rows is the select menu for selecting the cities. Help needed. Open to suggestions and improvements.
link to actual code in jsfiddle http://jsfiddle.net/NAafu/10/
In your case maybe you should also clone the events:
$('#details').clone(true).appendTo('#dailyexpense');
as stated in the documentation of clone()
EDIT - The problem perhaps is that you are using an id selector ($("#cities").live) which returns only one element. Ids should be unique on the page you should use a class instead