php did not send data by using jquery - php

This is a code with DropDown list which can update the database upon on change, it doesn't work for some reason. I realize that the value $ gp_name didn't send out, can someone help me to fix it?
<select name='status' id='status'>
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Inquiry">Inquiry</option>
</select>
<input type="hidden" name="gp_name" id="gp_name" value="<?php echo $gp_name;?>" />
<div id="autosavenotify"></div>
<script>
$(document).ready(function() {
var gp_name = $('#gp_name').val();
$('select').on('change', function() {
var statusVal = $(this).val();
var gp_name = $('#gp_name').val();
alert(statusVal,gp_name);
$.ajax({
type: "POST",
url: "save.php",
data: {
statusType: statusVal,
gp_name: gp_name
},
success: function(msg) {
$('#autosavenotify').text(msg);
}
})
});
});
</script>
save.php
<?php
require_once("connMysql.php");
$gp_name=$_POST['gp_name'];
$st=$_POST['statusType'];
$qry ="UPDATE lotus_gp SET status='".$st."' where gp_name='".$gp_name."'";
$done = mysql_query($qry);
if($done)
{
echo $gp_name;
echo $st;
}
?>

First, check if the data is getting from your front-end sides on your PHP page.
if(isset($_POST['gp_name']) && isset($_POST['statusType'])){
$gpname = $_POST['gp_name']; // echo it to ensure
$satype = $_POST['statusType']; // echo it to ensure
}
If the data is not printed then you might have an issue with inputs.

Related

Not Getting a POST value from ajax

I have a select dropdown and a textfield in my form. So i tried using $.ajax whenever i choose a value in dropdown, the textfield will show an output based on the selected value from the dropdown. But im always getting an error undefined variable from ff.php. I tried to pin down the cause, and what i found is im not getting any POST value in ff.php. Any solution for this ? Thanks in advance.
Here is my code in prs.php
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "ff.php",
data:'productid='+val,
success: function(data){
$("#brandss").val(data);
}
});
}
</script>
<td><select name="drpcode" onchange="getState(this.value)" class="form-control name_list"><?php $drp = mysqli_query($conn,"SELECT productcode FROM products"); while ($dp = mysqli_fetch_array($drp)) {
?><option value="<?php echo $dp['productid']; ?>"><?php echo $dp['productcode']; ?></option><?php } ?></select></td>
<td><input type="text" id="brandss" name="brand" placeholder="Brand" class="form-control name_list" required value=""></td>
Here my codes in ff.php
<?php
require_once("conn.php");
$id = $_POST['productid'];
$query = mysqli_query($conn,"SELECT productbrand FROM products WHERE productid = '$id' ");
while($rs = mysqli_fetch_array($query,MYSQLI_BOTH)) {
$brand = $rs['productbrand'];
}
echo $brand;
?>
Please add the data in json format and specify the datatype as json in ajax function
$.ajax({
url: "ff.php",
type: "POST",
data: {
productid : val
// more fields can be added here
},
dataType: 'json',
success: function(return){
// process success
},
error: function(err) {
// Process failure
}
});
I think the format of ajax data attribute is wrong . You should try this :
data:{productid:val}
UPDATE :
you should select both productid and product code in select statement
<td>
<select name="drpcode" onchange="getState(this.value)" class="form-control
name_list">
<?php $drp = mysqli_query($conn,"SELECT productid,productcode FROM
products");
while ($dp = mysqli_fetch_array($drp)) {
?><option value="<?php echo $dp['productid']; ?>"><?php echo
$dp['productcode']; ?></option><?php } ?>
</select>
</td>
function getState(val) {
$.ajax({
type: "POST",
url: "ff.php",
data:{productid:val},
success: function(data){
$("#brandss").val(data);
}
});
}
Try running the query before select and then display result with loop-
<?php $drp = mysqli_query($conn,"SELECT productcode FROM products"); ?>
<select name="drpcode" onchange="getState(this.value)" class="form-control name_list">
<?php while ($dp = mysqli_fetch_array($drp)) { ?>
<option value="<?php echo $dp['productid']; ?>"><?php echo $dp['productcode']; ?>
</option><?php } ?>
</select>

Ajax is not calling the php file?

I am trying to get the price of Item after selecting the item in a dropdown. I have used ajax calls but my code is not going inside $.ajax({.... What is wrong with my code?
I have used alert in the php file but nothing displays, which means my code is not calling php file.
<script type='text/javascript'>
$("#puja_name2").on('change',function()
{
var id=$(this).val();
var data = 'id='+ id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
data: data,
cache: false,
success: function(html)
{
$("#puja_price2").html(data);
}
});
});
</script>
<div class="form-group">
<label>
Puja Name<span class="font-red">* </span>:
</label>
<select class="form-control" name="puja_name2" id="puja_name2" data-validetta="required">
<option value="">Select Puja Name</option>
<?php
$SQL_STATEMENT_puja = $DatabaseCo->dbLink->query("SELECT * FROM puja_type ");
while($DatabaseCo->dbRow = mysqli_fetch_object($SQL_STATEMENT_puja)){
?>
<option value="<?php echo $DatabaseCo->dbRow->puja_id; ?>" ><?php echo $DatabaseCo->dbRow->puja_name; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label> Price <span class="font-red">* </span>:</label>
<select id="puja_price2" >
</select>
</div>
You must change in jquery puja_name to puja_id:
$("#puja_id").on('change',function()
A couple things wrong with your code bud:
1) You have
$(document).ready(function(){
<script>
//code
</script>
}
What your code should look like is:
<script type="text/javascript">
$(document).ready(function() {
//code
});
</script>
If you need to add comments in your JS code, DO NOT use <!-- -->. Instead use // like you see in my code above.
2) You need to change in your jquery puja_name to puja_id because your <select></select> id is puja_id so:
this
$("#puja_name").on('change',function() {
turns to this
$("#puja_id").on('change',function() {
3) I'm not the greatest at SQL statements but I do feel that using the code below would help you even more (you don't HAVE to).
<?php
$SQL_STATEMENT_puja = "SELECT * FROM puja_type WHERE status='APPROVED' ORDER BY puja_name ASC";
$DatabaseCo = $conn->query($SQL_STATEMENT_puja);
$puja=$row['puja']; //This should actually be $puja=$row['puja_id'];
while($row = $DatabaseCo->fetch_assoc()) {
?>
<option value="<?php echo $row['puja_id']; ?>" <?php if($row['puja_id']==$puja) { echo "selected"; } ?>><?php echo $row['>puja_name']; ?></option>
<?php
}
?>
4) In your JQuery you have it as $("#puja_price").html(html); it should be $("#puja_price").html(data);
5) I also see that you have it as #puja_price but there's nothing in your html that has the id of puja_price so
I recommend changing this
$("#puja_price").html(html); to
$("#puja_price2").html(data);
6) Your JQuery code is
$.ajax
({
When it should be: $.ajax({
Full code:
<div class="form-group">
<label>
Puja Name<span class="font-red">* </span>:
</label>
<select class="form-control" name="puja_id" id="puja_id" data-validetta="required">
<option value="">Select Puja Name</option>
<?php
//If you already have the connection setup you don't need to add this
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$SQL_STATEMENT_puja = "SELECT * FROM puja_type WHERE status='APPROVED' ORDER BY puja_name ASC";
$DatabaseCo = $conn->query($SQL_STATEMENT_puja);
$puja=$row['puja'];
while($row = $DatabaseCo->fetch_assoc()) {
?>
<option value="<?php echo $row['puja_id']; ?>" <?php if($row['puja_id']==$puja) { echo "selected"; } ?>><?php echo $row['>puja_name']; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label>
Price <span class="font-red">* </span>:
</label>
<input type="text" class="form-control " id="puja_price2" name="puja_price2" disabled>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#puja_id").on('change',function() {
var id=$(this).val();
var data = 'id='+ id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
cache: false,
data: data,
success: function(html) {
$("#puja_price2").html(data);
}
});
});
});
</script>
Let me know if the changes work for you
Try this in your JS code.
<script src="jquery.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#puja_id').change(function(){
var id = $(this).val();
var data = 'id='+id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
data: data,
cache: false,
success:function(res){
$('#puja_price2').html(res);
}
});
});
});
</script>

Ajax not working on select tag, PHP, AJAX

I'm trying to use ajax on a select tag with 2 options, but it's not getting the $_POST for some reason. It prints out the "---", but it does not print out the $_POST value, which is either 1 or 2. I'm not sure what I did wrong. Please take a look at my code:
newtest.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(url,type,theName,id) {
$.ajax({
type: "POST",
url: url,
data: { select: $(type+'[name='+theName+']').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( id ).innerHTML = data;
}
});
}
</script>
<?php
echo "<select name = 'name' onchange = 'ajax(\"newtestx.php\",\"input\",\"name\",\"output\")'>";
echo "<option value = '1'> 1 </option>";
echo "<option value = '2'> 2 </option>";
echo "</select>";
echo "<div id = 'output'></div>";
?>
newtestx.php
<?php
$name = $_POST['name'];
echo $name."---";
?>
You are sending a POST parameter with the key "select" to the server in your AJAX call:
data: { select: $(type+'[name='+theName+']').val()},
In newtestx.php you are trying to retrieve the value from a POST parameter with the key "name" - which doesn't exist:
$name = $_POST['name'];
You could fix this easily by giving the parameter keys the same name. If you would look for $name = $_POST['select'] the parameter would be found.
Inline Javascript is considered bad practice and there's no need to echo out the HTML markup, it makes the mark up harder to work with.
newtest.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="[link to your javascript file here]"></script>
<select name='numbers' class="postValueOnChange" data-id-to-update="output" data-post-url="newtestx.php">
<option value='1'>1</option>
<option value='2'>2</option>
</select>
<div id='output'></div>
Javascript file
$(document).ready(function () {
$('.postValueOnChange').change(postSelectedValue);
function postSelectedValue(e) {
var $self = $(this);
var url = $self.data('post-url');
var $elementToUpdate = $('#' + $self.data('id-to-update'));
var jqxhr = $.ajax({
type: "POST",
url: url,
data: {
selected: $self.val()
}
});
jqxhr.done(function (data) {
$elementToUpdate.html(data);
});
jqxhr.fail(function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
});
}
});
newtestx.php
<?php
$name = $_POST['selected'];
echo $name."---";
?>
You are sending post param select and trying to receive as $_POST['name'].
Make sure they match...either both as name or as select
First, Since you are using jQuery, why are you still using inline javascript?
Well I suggest you first to restrucure your code around the jQuery change event:
$(document).ready(function() {
$('select').change(function(e) {
e.preventDefault();
var selected = $('.select option:selected').val();
var id, theName, url= ...// get it from the DOM
$.ajax({
type: "GET",
url: url,
data: { select: selected},
error: function(xhr,status,error){alert(error);},
success:function(data) {
$('#'+id).html(data);
}
});
});
});
Second, why are you coding HTML with PHP, you are making yourself struggle and lose time only with quotes and double quotes, and no-needed spaces.
<form action="">
<select name="name">
<option value="1">1</option>
<option value="1">1</option>
</select>
</form>
<div id="output"></div>

How to populate 2 selectbox from database using php and ajax

I have a problem, how to pass an argument with ajax in url: <?php echo base_url() . 'user_m/getAdministrator'; ?>" argument,
Because I tried to populate a selectbox based on another but I didn't have success.
html:
<div class="form-group">
<label>Denumirea intreprinderii:</label>
<select class="form-control marg-left-10" id="firm">
<?php if($all_firms): ?>
<?php foreach($all_firms as $firm): ?>
<option value="<?php echo $firm['id_firm']; ?>"><?php echo $firm['name_firm']; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<div class="form-group">
<label>Administrator</label>
<select class="form-control marg-left-10" id="admin">
</select>
</div>
php:
public function getAdministrator()
{
$id_firm = $_POST['id_firm'];
$this->load->database();
$sql = $this->db->query("SELECT * FROM users,firms WHERE firms.id_firm = users.fk_firm and users.id = ");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['name'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
return $this;
}
script:
$(document).ready(function()
{
$("#firm").change(function()
{
var id_firm=$(this).val();
var dataString = 'id_firm='+ id_firm;
$.ajax
({
type: "POST",
url: "<?php echo base_url() . 'user_m/getAdministrator'; ?>",
data: dataString,
cache: false,
success: function(html)
{
$("#admin").html(html);
}
});
});
});
Help me please.The first select box gets populated but the second doesn't.Please Help me.Exist a method to pass an argument to user_m/getAdministrator method? HELP ME PLEASE GUYS!!!!!
Php in js is not a good practice, try to write this on your main php file before calling the script
<script>
var base_url = "<?=base_url()?>";
</script>
and then in your js file:
$.ajax
({
type: "POST",
url: base_url+"user_m/getAdministrator",
data: dataString,
cache: false,
success: function(html)
{
$("#admin").html(html);
}
});
also, you can send data this way:
data: { id_firm: id_firm },

What's wrong with this Jquery script?

Trying to populate a drop down list for cities once a user makes a selection for the state using Jquery/Ajax functionality
HTML Form has...
<?php $allRegions = Region::newInstance()->getStatesByCountry('US'); ?>
<select name="regionId" id="regionId">
<?php foreach($allRegions as $region) { ?>
<option value="<?php echo $region['value'] ; ?>"><?php echo $region['name'] ; ?></option>
<?php } ?>
</select>
<select name="cityId" id="cityId">Choose a city</select>
Jquery code...
$(document).ready(function(){
$("#regionId").change( function() {
var regionId = $(this).val();
var url = '<?php echo site_base_url(true)."?page=ajax&action=cities&regionId="; ?>' + regionId;
$.ajax({
type: "GET"
url: url,
dataType: 'json',
success: function(msg){
if (msg != ”){
$("#cityId").html(msg).show();
}
}
});
});
});
</script>
The issue is that on selecting a region the cities drop down select doesn't get populated ... can't figure it out.. any help is appreciated.
One problem is that you have a strange quote character:
if (msg != ”){
You probably mean this:
if (msg != ''){
and another problem is:
type: "GET"
that should be:
type: "GET",
Not to be rude, but your code is very nested, and unecessary complicated.
Fix all the mentions above from the others, and also this:
<?PHP
foreach($allRegions as $region) {
<option value="<?php echo $region['value'];" . "><" . echo $region['name']; .
"</option>" } ?>
</select>

Categories