Select database table depending on select box value (php + Ajax) - php

Okay this is driving me crazy now, I can't seem to find a solution at all!
Basically I have two select boxes on my page, However I want the second select box to to be populated from a mysql database depending on the value of the first select box.
For example if I choose the value 'misc' from the first select box, the second select would be populated with the misc table from my database! (the second select box will be refreshed via ajax on first select box choice change)
My only problem is I can't think of a way to store the value of the first select box in a php variable, so it can be used in my function to populate the second select box(which is refreshed by ajax)
Thanks guys
<form action="scripts/tp/process.php" method="post" enctype="multipart/form-data">
Select teleport here:
<select name="selectmate" id="Testid" onchange='Ajax();'>
<option value="Misc">Misc...</option>
<option value="Stunt">Stunt</option>
<option value="deathmatches">Deathmatches</option>
<option value="Car"> Car </option>
<option value="Races">Races </option>
</select>
<br/>
<label for="file">Filename:</label>
<input name="file" type="file"/>
<br/>
<label for="Name">Teleport name:</label>
<input type="text" name="Name" id="Name"/>
<br/>
<label for="Tp"> Teleport command:</label>
<input type="text" name="Tp" id="Tp"/>
<br/>
<label for="Info">teleport infomation</label>
<input type="text" name="Info" id="Info"/>
<br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<h2>Delere entry here!</h2>
<form action="scripts/misc/miscdelete.php" method="post" enctype="multipart/form-data">
<select name="deletemisc">
<?php
ListDatabase($value);
?>
</select>
<input type="submit" name="delete" value="Delete"/>
</form>
I need to store the value of the first select box in the variable $value

$("#Testid").change(function () {
var val = $("#Testid option:selected").text();
// here your ajax request
// Fill second select here:
var deletemisc = $("#deletemisc");
$("#deletemisc > option").remove();
$.post("/path/to/script", { selected_val: val}, function(result) {
$.each(result, function (item) {
deletemist.append($("<option />").val(item.val).text(item.text));
})
});
});​
"/path/to/script" - must return json array of vals.

Related

How to update a forms text fields when a drop gets updated using php

I have a form in a php page that I want to have updated as the drop down is changed. Here is the code for the form:
<form action="AdminPage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="action" value="version" />
<label><strong>Name:</strong></label>
<select id="WebName" name="Name">
<option value="ReportDashboard">ReportDashboard</option>
<option value="ReportDashboard Dev"<?php if(strpos($_SERVER['PHP_SELF'],"dev/FrontierReports",1) > 0){echo " selected='selected'";}?>>ReportDashboard Dev</option>
<option value="ReportDashboard Testing"<?php if(strpos($_SERVER['PHP_SELF'],"dev/Testing",1) > 0){echo " selected='selected'";}?>>ReportDashboard Testing</option>
</select><p></p>
<label><strong>Version Number: </strong></label>
<input id="VersionNumber" type="text" name="VersionNumber" value="<?php echo $VersionNumber['VersionNo']; ?>" /><p></p>
<label><strong>Version Type:</strong></label>
<select id="Type" name="VersionType">
<option value="1">Major</option>
<option value="2">Minor</option>
<option value="3" selected="selected">Bug</option>
</select><p></p>
<input type="hidden" name="VersionNumberCheck" value="<?php echo $VersionNumber['VersionNo']; ?>" />
<label><strong>Notes</strong></label>
<input id="Notes" type="text" name="Notes" value="<?php echo $ReleaseNotes['ReleaseNotes']; ?>" />
<p><input class="Pointer" type="submit" name="submit" value="Update Version" /></p>
<p><input id="VersionSubmit" class="Pointer" type="button" name="VersionSubmit" value="Current Notes" /></p>
</form>
Currently, the form gets its values for the 2 text fields (VersionNumber and Notes) from outside the form. I would like to have them be obtained as the form is created based on what the first drop down (Name) has selected and then update if that drop down gets changed.
I thought of adding some PHP to the form to help, but it doesn't work. Here is what I tried:
<form action="AdminPage.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="action" value="version" />
<label><strong>Name:</strong></label>
<select id="WebName" name="Name">
<option value="ReportDashboard">ReportDashboard</option>
<option value="ReportDashboard Dev"<?php if(strpos($_SERVER['PHP_SELF'],"dev/FrontierReports",1) > 0){echo " selected='selected'";}?>>ReportDashboard Dev</option>
<option value="ReportDashboard Testing"<?php if(strpos($_SERVER['PHP_SELF'],"dev/Testing",1) > 0){echo " selected='selected'";}?>>ReportDashboard Testing</option>
</select><p></p>
<?php
$Vsql = "select top 1 VersionNo,ReleaseNotes from pmdb.Version where Name = 'ReportDashboard' order by name,VersionNo desc";
$GetVersion = $conn->query($Vsql);
$VersionNumber = $GetVersion->fetch(PDO::FETCH_ASSOC);
?>
<label><strong>Version Number: </strong></label>
<input id="VersionNumber" type="text" name="VersionNumber" value="<?php echo $VersionNumber['VersionNo']; ?>" /><p></p>
<label><strong>Version Type:</strong></label>
<select id="Type" name="VersionType">
<option value="1">Major</option>
<option value="2">Minor</option>
<option value="3" selected="selected">Bug</option>
</select><p></p>
<input type="hidden" name="VersionNumberCheck" value="<?php echo $VersionNumber['VersionNo']; ?>" />
<label><strong>Notes</strong></label>
<input id="Notes" type="text" name="Notes" value="<?php echo $ReleaseNotes['ReleaseNotes']; ?>" />
<p><input class="Pointer" type="submit" name="submit" value="Update Version" /></p>
<p><input id="VersionSubmit" class="Pointer" type="button" name="VersionSubmit" value="Current Notes" /></p>
</form>
I don't know how to capture what is in the drop down to change the SQL that is pulling the data. I thought of possibly just pulling all of the rows back and then doing a search against it once the form is built, but I still don't know how to capture when the drop down changes and then update the 2 text boxes.
UPDATE
I now have a new file named UpdateAdminVersion.php this is what is has:
$Vsql = "select top 1 VersionNo,ReleaseNotes from pmdb.Version where Name = 'ReportDashboard' order by name,VersionNo desc";
$GetVersion = $conn->query($Vsql);
$VersionNumber = $GetVersion->fetch(PDO::FETCH_ASSOC);
$data = $VersionNumber;
echo json_encode($data);
I also added the following script to the previous file with the form:
$("#WebName").change(function ()
{
$.post("UpdateAdminVersion.php",
{
parameter1: "ReportDashboard Dev",
parameter2: $("#VersionNumber").val()
},
function (data, status)
{
aconsole.log(data)
$("#Notes").val(data);
});
});
I must still be missing something because nothing happens when I change the drop down.
UPDATE 2
I've changed the script to this:
$("#WebName").change(function ()
{
$.post("UpdateAdminVersion.php",
{
WebName: $('#WebName').val(),
VersionNumber: $("#VersionNumber").text()
},
function (data, status)
{
console.log(data)
$("#Notes").val(data);
success: alert ("Number 2 is successful!");
});
success: alert ("Number 1 is successful! \n" + WebName + "\n" + VersionNumber);
});
I get the alert Number 1 is successful!, but the WebName and VersionNumber variables return [object HTMLSelectElement] and [objectHTMLInputElement] respectively. How do I return the actual values?
And how do I use them in the UpdateAdminVersion.php file?
For starters you can use jqueryUI Autocomplete to feed Select tag with options.
Then use onchange event to capture dropdown changes.
<select onchange="myFunction()">
when event is triggered retreive data from db using Ajax, on success modify your form fields accordingly.
Look at this Fiddle give your own parameters, should work.

Associate input type checkbox to input type text

I have a problem, I need to associate a input type checkbox to a input type text.
The situation is as follows:
Extract data from a database. The PK data are the value of the checkbox. When a checbox select an input type text where you can enter a specific number is activated.
Now the problem is that selecting a checkbox input text all kinds are activated. And what I hope is that by selecting the checkbox input only the input associated with the checkbox enabled.
My HTML code (This code creates a input checkbox and input text for each record in the database, and what I want is to activate a specific checkbox is activated, the input type text):
<form action="send.php" method="POST" id="form-touch">
<?php foreach ($data as $key): ?>
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="<?php echo $key['pr_id'] ?>">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<?php endforeach; ?>
<input type="submit" value="Send">
</form>
My jquery code (code that activates or deactivates the text field by selecting a checkbox):
$('.id-check').change(function(){
if ($('.id-check').is(':checked') == false){
$('.myinput').val('').prop('disabled', true);
} else {
$('.myinput').val('1').prop('disabled', false);
}
});
How I can achieve what I want? Greetings from Chile.
Try
$('.id-check').change(function() {
if ($(this).is(':checked') == false) {
$(this).closest('div').find('.myinput').val('').prop('disabled', true);
} else {
$(this).closest('div').find('.myinput').val('1').prop('disabled', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="send.php" method="POST" id="form-touch">
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="1">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<input type="submit" value="Send">
</form>
you can use .next() jQuery selector. I have modified your jQuery change handler below. It works for me so try it. It should work for you too.
$('.id-check').change(function(){
if ($(this).is(':checked') == false){
$(this).next().val('').prop('disabled', true);
} else {
$(this).next().val('1').prop('disabled', false);
}
});
Goodluck and happy coding! :-)

Load JSON data through jQuery and PHP

I am trying to send a textbox value (invoice number) to a PHP file through jQuery and AJAX. From the PHP file, I am returning the JSON encoded data. I want to display the values fetched from the database in the following textboxes (Total Amount, Balance Amount) and select boxes (Payment mode, Payment status).
I am not getting the output I wish. Where am I going wrong?
HTML:
<form name="inv_payment" method="post" action="" style="margin:0px;width:400px;">
<fieldset>
<legend>Payment details</legend>
<label for=inv_num>Invoice Number</label>
<input id=inv_num name=inv_num type=number placeholder="12345" required autofocus >
<input type=button name="get_details" id="get_details" value="Get Details" >
<label for=tot_amt>Total Amount</label>
<input id=tot_amt name=tot_amt type=text placeholder="12345" disabled > <br/>
<label for=pay_up>Pay Up </label>
<input id=pay_up name=pay_up type=text placeholder="12345" required ><br/>
<label for=bal_amt>Balance Amount </label>
<input id=bal_amt name=bal_amt type=text placeholder="12345" disabled > <br/>
<label id="paymt_mode"><strong>Payment Mode</strong></label>
<select name="pay_mode" style="width: 130px">
<option selected="selected">Cash</option>
<option>Cheque</option>
</select><br/>
<label id="paymt_status"><strong>Payment Status</strong> </label>
<select name="pay_status" style="width: 130px">
<option selected="selected">Pending</option>
<option>Completed</option>
</select><br/>
<input type="submit" value="Payment" name="pay_btn">
</fieldset>
</form>
jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#get_details').click(function() {
//To pass the invoice number and fetch related details
$.getJSON("get_invpay_details.php", {id: $('#inv_num').val()}, function(data){
$('#tot_amt').val(data['Total_Amount']);
$('#bal_amt').val(data['Balance_Amount']);
$('#pay_mode').val(data['Payment_Type']);
$('#pay_status').val(data['Payment_Status']);
});
});
</script>
PHP:
<?php
include('includes/dbfunctions.php');
include('includes/db.php');
if(isset($_GET['id']))
{
$tmp = $_GET['id'];
$sql = mysql_query("select Total_Amount,Balance_Amount,Payment_Type,Payment_Status from invoice where InvoiceNum='$tmp'");
if(mysql_num_rows($sql))
{
$data = mysql_fetch_array($sql);
echo json_encode($data);
}
}
?>
Your json response value populate with object.data like this.
$('#tot_amt').val(data.Total_Amount);
$('#bal_amt').val(data.Balance_Amount);
$('#pay_mode').val(data.Payment_Type);
$('#pay_status').val(data.Payment_Status);
Here data is an object not a array.
I have checked my query.It gave me the correct output in mysql editor.
what i could see is.. you have a typo in your question...
missing }); for document.ready..which might be breaking your script..(this should have error in your console though..)..anyways something to check about..
$(document).ready(function() {
$('#get_details').click(function() {
//To pass the invoice number and fetch related details
$.getJSON("get_invpay_details.php", {id: $('#inv_num').val()}, function(data){
$('#tot_amt').val(data['Total_Amount']);
$('#bal_amt').val(data['Balance_Amount']);
$('#pay_mode').val(data['Payment_Type']);
$('#pay_status').val(data['Payment_Status']);
});
});
});
//^^--here

how do i send drop down select values to another page i have code but i can't get drop down select values

how do i send drop down select values to another page i have code but i can't get drop down select values into another page? i want to send size values to another page header('location:shoppingcart.php?size='.$sizes.''); also not working. i have tried form action on size form then addtocart not working.. problem with button when i used submit button instead of button then it's working but with submit button add to cart function will not work?
is that possible i can send $_POST['size'] values to another page without submit form?
*javascript *
<script language="javascript">
function addtocart(id){
document.form1.id.value=id;
document.form1.command.value='add';
document.form1.submit();
}
</script>
Php Code
$sizes=$_POST['size'];
include("cart/functions.php");
if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
$id=$_REQUEST['id'];
addtocart($id,1);
header('location:shoppingcart.php?size='.$sizes.'');
exit();
}
Form 1 for Add To Cart
<form name="form1" method="post">
<input type="hidden" name="id" />
<input type="hidden" name="command" />
</form>
Size Form
<form action="" id="size1" name="size1" method="post">
<div class="details clearfix">
<p class="item">
<label style="font-family:Arial,Helvetica,sans-serif; font-size:12px; font-weight:bold; ">Choose your size:</label>
<select style="background-color:#CCC" name="size" id="size" >
<option>Select</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<option value="XL">XL</option>
</select>
</div>
Add To Cart Button
<div class="buy">
<?php
$query3=mysql_query("select * from products WHERE id='$id' ");
$row3=mysql_fetch_array($query3);
?>
<input type="button" class="button1" value="Add To Cart" >
</div>
</form>
If you just want to send the data to some other page as soon as the drop down values changes then you can try like this with jQuery:
$("#size").change(function(){
var dropdown_val=$(this).val();
$.post("shoppingcart.php", { size:dropdown_val},function(data){
console.log(data);
} );
});
on the shoppingcart.php side you can receive the value in $_POST['size']
Use the action event
<form action="process.php" id="size1" name="size1" method="post">
I don't know if this is a school project or something weird you are working on or just very early stages, but the whole page looks very odd in the way it is written.

reading from input tag outside form element

I have a web page with an input field (a calendar to select a date from).
The problem is that I need to read that input field from 2 different form elements that each have their own action and other values.
|calendar| [do this] [do that]
I don't want to copy the input field in both forms since it would look silly.
Any suggestions?
Edit: here's the two forms:
<form action="/adm/user/credit/update?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="isPaid" value="1"/>
<label for="datums">Datums: </label>
<input type="text" id="datums" name="datums"/>
<input type="submit" value="Paid" style="background-color:green; color:white"/>
</form>
<form action="/adm/user/credit/extend?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="phone" value="<?=$phone?>">
<select name="period">
<option value="7">7</option>
<option value="14">14</option>
<option value="30">30</option>
</select> days
<input type="submit" value="Extend" style="background-color: blue; color: white">
</form>
Just give it unique ID and use document.getElementById like this anywhere you want:
var myValue = document.getElementById("myInputID").value;
This way it doesn't matter where the input is located, it can even be outiside of any form.
Edit:
To read the value upon submit, first add the onsubmit part to the relevant form:
<form action="....." onsubmit="ReadValue(this);">
And now have this in your page:
<script type="text/javascript">
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
alert("value is: " + myValue);
}
</script>
This will show the value as alert dialog.. if you want to populate hidden form field with the value, have such code instead:
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
oForm.elements["myHiddenFieldId"].value = myValue;
}
Hope it's clear enough! :)

Categories