In my index file where the form and function are located, named:
index.php
$(function() {
$.ajax({
type: "POST",
url: "invtype.php",
data: "getrevenuetype=true",
success: function(b){
$("#revenueType").html(b)
}
})
})
<span name="revenueType" id="revenueType"></span><br/>
invtype.php
<?php
mysql_connect("","","") or die('Error: '.mysql_error());
mysql_select_db("dbname");
if(isset($_POST['getrevenuetype'])) {
$sql3 = mysql_query("SELECT * FROM chartofaccount WHERE accountnumber >= 4000 and accountnumber <= 4999");
$acct = '';
$acctrow = mysql_num_rows($sql3);
if($acctrow > 0) {
echo 'Revenue Type:<select name="rename">';
while($chartrow = mysql_fetch_array($sql3)) {
$revenueaccountname = $chartrow['accountname'];
$acct .= '<option value="$revenueaccountname"> '. $revenueaccountname .' </option>';
}
}
}
echo $acct;
echo '</select>';
My question is how will I get or what code should I put on to get the value of the option selected by the user? My purpose in getting the value is that I want to put it in another php where it will be used as var in inserting data into MySQL. already tried this code: $name = $_POST['rename']; then included 'invtype.php'; on the other php file (this is where I will use the value of option selected) but it doesn't seem to work. I know my code is all messed up and I'm really sorry about it, but guys if you can help I would really appreciate it.
you need to add , for example, ' for first time response
echo $acct;
echo '</select>';
echo '<input type="submit id="submit" value="" />';
than add js
$('#sumbit').click(function(){
var name = $('select[name=rename]').val();
$.ajax({
type: "POST",
url: "invtype.php",
data: "rename=1&name="+name,
success: function(b){
$("#revenueType").html(b)
}
})
});
than change invtype.php
if(isset($_POST['rename'])) {
$name = $_POST['name'];
//work with base
}
How's this?
Give your form an id attribute and replace the formid with it :)
$(function() {
$.ajax({
type: "POST",
url: "invtype.php",
data: "getrevenuetype=true&selectedoption=" + $('#formid option:selected').val(),
success: function(b){
$("#revenueType").html(b)
}
})
})
After the select tag is added to the page and an option is selected by the user, you can then send the selected value with a button click like this:
$("#btn").click(function(){
$.ajax({
type: "POST",
url: "server.php",
data: "value=" + $('option:selected').val(),
success: function(b){
alert('Sent successfully!');
}
})
})
$('select[name="rename"] option:select').val()
Related
I got two dropdowns and i need to get both values to get data from my database.
$(document).ready(function(){
$('.fabric').on('change',function(){
var fabricID = $(this).val();
console.log("fabric id_price is " + fabricID); //debugging
if(fabricID){
$.ajax({
type:'GET',
url:'cart_functions.php',
dataType: 'json',
data: {
fabric_id: fabricID
},
success:function(html){
$('.icms' + id).text(data.val);
}
});
//closing tags
$('.size').on('change',function(){
var sizeID = $(this).val();
if(sizeID){
$.ajax({
type:'GET',
url:'cart_functions.php',
dataType: 'json',
data:{
size_id: sizeID
},
success:function(html){
$('.icms' + id).text(data.val);
}
});
//closing tags
i'm sending these both values to my calculate.php
<?php header('Content-Type: application/json');
include_once '../incluedes/conn_cms.php';
if(isset($_GET["size_id"],$_GET["fabric_id"])){
$size_id=$_GET["size_id"] ;
$fabric_id=$_GET["fabric_id"] ;
$query3 =" SELECT * FROM valores_almofadas
WHERE size='$size_id'
AND price_id ='$fabric_id'";
$result = mysqli_query($conn,$query3);
while($rows = mysqli_fetch_assoc($result)){
if($_SESSION['estado'] == 'SP'){
$ICMS = $rows['icms_7'];
}else{
$ICMS = $rows['icms_12'];
}
$_SESSION['icms']=$ICMS;
} echo json_encode($_SESSION['icms']);
}
?>
So i select a fabric and then a size fabric value is my id and size is 50 or 45.
fabricid= 1 and size = 50 <-- i am sending this to my calculate.php
So i want to get back the value into a session.
and the result must be on a td..
<td class="icms'.$id.'">R$:'.$_SESSION['icms'] .'</td>
But its not working, i'm not good at ajax, can you tell me whats wrong and how can i fix these mess?
Make sure both values are always sent with the request
$(document).ready(function() {
$('.fabric, .size').on('change', sendData);
function sendData() {
var fabricID = $('.fabric').val();
var sizeID = $('.size').val();
if ( fabricID !== "" && sizeID !== "") {
$.ajax({
type : 'GET',
url : 'cart_functions.php',
dataType : 'json',
data : {
fabric_id: fabricID,
size_id: sizeID
}
}).done(function(html) {
$('.icms' + this.id).text(data.val);
});
}
}
});
You are never sending both values in any ajax call, only one or the other. You need to additionally get the value for fabric_id in your .size change event and vice versa.
I want to capture value keyed in textbox for quantity in the shopping cart while they type. I used jquery's key up for this.Then I want to save the new value in the session for quantity. I'm getting the value but unable to save it. It returns null after refreshing the page.
Html
<input type="text" size="4" class="qty" value="<?php echo $v['quantity'];?>" id="<?php echo $k;?>"/>
Ajax
$("input[class=qty]").keyup(function()
{
var data;
var newVal = $(this).val();
var id = this.id;
console.log(id+" "+newVal);
$.ajax({
type: "GET",
dataType: "text",
url: "updateCart.php?id="+id+"&&value"+newVal, //Relative or absolute path to response.php file
data: data,
success: function(data) {
console.log(data);
newVal=data;
}
});
});
updateCart.php
<?php
session_start();
$id = $_GET['id'];//say 2
$new_value =$_GET['value'];//say 16
foreach($_SESSION['cart'] as $k=>$v)
{
$k;
$v["id"];
$v["quantity"];
if($id == $k)
{
//assign new value to the quantity
$_SESSION['cart'][$id]['quantity']=$new_value;
//print_r($_SESSION['cart'][1]['quantity']);
}
}
//echo json_encode($new_value);
//var_dump($_SESSION['cart']);
this can display session['cart'] with updated value if I test in
this page ..but when passed to originated page via ajax, returns null.
echo $_SESSION['cart'][$id]['quantity'];
?>
You left out the = between &value and + newVal. But I recommend you let jQuery do this automatically by using the data: option.
data = { id: id, value: newVal };
$.ajax({
type: "GET",
dataType: "text",
url: "updateCart.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
console.log(data);
newVal=data;
}
});
I have a javascript coding that pulls a PHP file, and if it updates the database, it will echo Success: Age Updated! It works and all, But When I click it multiple times it will start to do this.. Sucsess: Age Changed! Sucsess: Age Changed! Sucsess: Age Changed! .. Here's the Javascript.
function UpdateAge(Age) {
var newage = $("#NewAge").val();
var dataString = 'newage=' + newage;
if (newage.length < 2) {
$('#Required').fadeIn(300);
$('#Mask').fadeIn(300);
} else {
$.ajax({
type: "POST",
url: "update_age.php",
data: dataString,
cache: false,
success: function (updateage) {
$("#UpdatedAge").append(updateage);
$("#UpdatedAge").show().fadeOut(1200);
}
});
}
}
How would I get it to just display Success: Age Updated! once, instead of it just multiplying everytime I click?
Here's the Button for the onClick
<input type="submit" value="Update" onClick="UpdateAge(<?php echo $age ?>)" />
Looks like you just need to empty your #UpdatedAge Element each time.
function UpdateAge(Age) {
var newage = $("#NewAge").val();
var dataString = 'newage=' + newage;
if (newage.length < 2) {
$('#Required').fadeIn(300);
$('#Mask').fadeIn(300);
} else {
$.ajax({
type: "POST",
url: "update_age.php",
data: dataString,
cache: false,
success: function (updateage) {
$("#UpdatedAge").empty();
$("#UpdatedAge").append(updateage);
$("#UpdatedAge").show().fadeOut(1200);
}
});
}
}
the problem is you keep appending, change the content instead (just change append to html)
$("#UpdatedAge").html(updateage);
Just use something as
function UpdateAge(Age) {
var newage = $("#NewAge").val();
var dataString = 'newage=' + newage;
if (newage.length < 2) {
$('#Required').fadeIn(300);
$('#Mask').fadeIn(300);
} else {
$.ajax({
type: "POST",
url: "update_age.php",
data: dataString,
cache: false,
success: function (updateage) {
$("#UpdatedAge").html(updateage);
$("#UpdatedAge").show().fadeOut(1200);
}
});
}
}
As .html replaces the content of #UpdatedAge and don't append it to the content, as .append does.
I am grabbing the values from a listbox and passing it on to another listbox, I had everything working with one value $Lid, but now I need two $Lid and $Cid, is this the correct way to do this?
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_area.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Pig").html(html);
}
});
});
});
</script>
</head>
<body>
<div id="frame1">
<label>Place :</label>
<select name="Doggie" class="Doggie" id="Doggie">
<option selected="selected">--Select Place--</option>
<?php
$sql = mysql_query("SELECT tblLocations.RestID as Lid, tblLocations.CityID as Cid, tblRestaurants.RestName as name
FROM tblRestaurants INNER JOIN tblLocations ON tblRestaurants.RestID = tblLocations.RestID
GROUP BY tblLocations.RestID, tblRestaurants.RestName
ORDER BY tblRestaurants.RestName ASC");
while($row=mysql_fetch_array($sql))
{
echo '<option value="'.$row['Lid'].''.$row['Cid'].'">'.$row['name'].'</option>';
} ?>
</select>
<label>City :</label>
<select name="Kitty" class="Kitty" id="Kitty">
<option selected="selected">--Select City--</option>
</select>
<label>Area: :</label>
<select name="Pig" class="Pig" id="Pig">
<option selected="selected">--Select Area--</option>
</select>
</div>
</body>
</html>
And...
<?php
require ('config.php');
if ($_POST['Lid']) {
$Lid = $_POST['Lid'];
$sql = mysql_query("SELECT tblLocations.RestId as Lid, tblLocations.CityID as Cid, tblCities.CityName as name
FROM tblLocations INNER JOIN tblCities ON tblLocations.CityID = tblCities.CityID
WHERE tblLocations.RestID = $Lid
GROUP BY tblLocations.RestID, tblCities.CityName
ORDER BY tblCities.CityName ASC");
echo '<option selected="selected">--Select City--</option>';
while ($row = mysql_fetch_array($sql)) {
echo '<option value="' . $row['Lid'] . '' . $row['Cid'] . '">' . $row['name'] . '</option>';
}
}
?>
Right now its not returning anything so I have to assume its wrong. Thank you.
I would recommend making the changes below:
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
You were adding two data values, which is not the right way of doing it. Simply pass a single literal object with your desired key and values and allow JQuery to do the formatting for you.
I don't understand why you store the same value in two vars:
var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();
It can be simplified to:
var LocationString = $(this).val();
And then you only have one value, so data should be the following format
data: {
'Lid': LocationString
}
data should be the format
data: {Lid : LocationString, Cid : CityString},
and also check what is the result of your query
check it by
print_r(mysql_fetch_array($sql))
if your query does not have any results , echo inside while loop wil not work
This did it.
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString ='Rid='+ $(this).val();
$.ajax({
type: "POST",
url: "place_city.php",
data: LocationString,
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(), // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
//alert("Rid = " + Rid + " Cid = " + Cid);
$.ajax({
type: "POST",
url: "place_area.php",
data: {"Rid":Rid,"Cid":Cid},
cache: false,
success: function (html) {
//alert('This is what is returned from the php script: ' + html);
$(".Pig").html(html);
}});});});
Ok, so I have this search box in which people typein a food item. When they press the button I need that input to be send to a .php file. That php file will look op the calories of that food item (thats in my database) and output the food item name and calories. All this need to be done without reloading the page so I started figuring out how JQuery works.
However I am stuck, I don't know what to put in the data field of the jquery function and how I can 'catch' that data in the .php file. Can someone give me an idea? thanks a lot! (see the ??????'s for things i don't understand). Also, the data that comes back needs not to be in an alert box in the end, but update some table on my page, how can i do this? which JSON (?) Jquery function do I need?
what I have up until now:
in head:
<script type="text/javascript">
function contentDisp()
{
$.ajax({
type: 'POST',
url: 'getFood.php',
data: '????????',
success: function(data){
alert("Data Loaded: " + data);
}
});
}
</script>
and in body:
<form autocomplete="off">
<p>
Product:
<input type="text" name="food" id="food" class="food_name_textbox" onmouseover="javascript: this.className='food_name_textbox_mouseover';" onmouseout="javascript: this.className='food_name_textbox';" / >
</p>
<button id="zoek" type="button" onClick="contentDisp();">Zoek</button>
</form>
and in getFood.php:
<?php
require_once "config.php";
$id = "??????"
$result = mysql_query("SELECT * FROM voedingswaarden WHERE voedsel='$id'");
$row = mysql_fetch_array($result);
echo json_encode($row);
?>
$.ajax({
type: 'POST',
url: 'getFood.php',
data: {'foodnametextbox' : $('#food').val() },
datatype: "json",
success: function(data){
$('#table').html(data);
}
});
<?php
require_once "config.php";
$id = $_POST['foodnametextbox']; //escape and validate this input before using it in a query
$result = mysql_query("SELECT * FROM voedingswaarden WHERE voedsel='$id'");
$row = mysql_fetch_array($result);
echo json_encode($row);
?>
<script type="text/javascript">
function contentDisp()
{
var textSearch = $("#myText").text();
$.ajax({
type: 'POST',
url: 'getFood.php',
data: textSearch,
success: function(data){
alert("Data Loaded: " + data);
}
});
}
</script>
PHP:
$id = $_POST['textSearch'];