undefined php data using ajax - php

I am trying to send 2 values from cx_card to my getproduto.php. For this I am using ajax
The variable values:
com_id=1
cliente_id=3440
The form select:
<select width="200" name="produto" id="produto-list" class="produtoInputBox" onChange="getsubproduto(this.value);" <?php echo $permissao; ?>>
ajax code
function getproduto(val,val_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente='+val_id,
success: function(data){
$("#produto-list").html(data);
}
});
}
My Query in getproduto.php
$query ="SELECT * FROM apolices WHERE id_cliente='".$_POST['cliente_id']
."' and id_companhia = '".$_POST['com_id']."'";
$results = $db_handle->runQuery($query);
In cx_card i have print the query i have recibe from getproduto.php the result is:
SELECT * FORM apolices WHERE id_cliente='undefined' and id_companhia='1'
Thanks for the help.

You're only passing one argument to your getproduto function. If you want two different values you need two different arguments. Try this:
function getproduto(com_id, cliente_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+com_id+'&cliente_id='+cliente_id,
success: function(data){
$("#produto-list").html(data);
}
});
}

You're only sending in one value:
function getproduto(val) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente_id='+val,
success: function(data){
$("#produto-list").html(data);
}
});
}
You should be sending two:
function getproduto(val,val_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente_id='+val_id,
success: function(data){
$("#produto-list").html(data);
}
});
}

Related

PHP AJAX - Pass a PHP value from one page to another using AJAX

I would like to pass a php variable onto another php page (category-get-secondary.php) through Ajax. I have the following code:
function getSecondaryCat(val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data: 'primary_cat='+val,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
On category-get-secondary.php I want to get the value: -
$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];
$postPrimaryCat is transferred. Now I want to transfer the value for $categoryType.
I would like to pass the PHP variable 'category-select'. This is what I tried to transfer the value:
function getSecondaryCat(val,cat_val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:'primary_cat='+val+'&category-select='+cat_val,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
and to get the value I'm using
$getCategorySet = $_POST['category-select'];
but the value doesn't seem to be transfereed
Updated with full code:
main-page.php
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
</script>
<script>
function getSecondaryCat(val, catval) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:'primary_cat='+val+'&categoryselect='+catval,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
function selectCountry(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
<?php
//Get category set from URL bar
$_POST['categoryselect'] = 'premium';
category-get-secondary.php
<?php
//Insert value to ajax
$postCatType = $_POST['categoryselect'];
Valid Characters
In general JavaScript, variable/function names can't contain -. They can only contain letters, $, and _ (Underscore)
So... change data:'primary_cat='+val+'&category-select='+cat_val, to this data:'primary_cat='+val+'&category_select='+cat_val,
Hope it solves your issue.
function getSecondaryCat(val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data: {primary_cat:val},
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
Change data and add below data line in your code.
Ajax
function getSecondaryCat(val,cat_val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:{primary_cat:val,category_select:cat_val} ,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
PHP
$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];

how to post multiple values to PHP function using AJAX?

I have this code:
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne},
success: function(response) {
alert(response);
}
});
It's working, but i need to do something like this:
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne, name},
success: function(response) {
alert(response);
}
});
It gives me errors like:
Warning: missing argument 2 for func1()
I am using this code too:
function func1($data, $name){
//some code here
}
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1']);
}
How to do it right?
Either create an array and pass as much as variable you want. otherwise try this:-
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne,"callFunc2": name},
success: function(response) {
alert(response);
}
});
You should add a name to your secund parameter in the AJAX call. Don't forgot to pass it ot your function "func1".
Example:
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1'], $_POST['callFunc2']);
}

How to Send Jquery Ajax data on Same php Page?

hi i want to send javascript data via ajax to the same page in wordpress. Cannot fetch the data in testajax.php page. its shows Undefined index: name. And how to send the data on same php page rather than testajax.php ?
<script>
jQuery.ajax({
type: "POST",
url: 'http://localhost/projects/xxx/wordpress/wp-content/themes/xxx/testajax.php',
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});
var foo = 'somename';
jQuery.ajax({
url:'yourUrl',
type: "POST",
data: {'name':foo},
success: function(data)
{
alert('success');
}
});
php
<?php
if(isset($_POST['name'])){
//Do whatever you want
}else{
//Do whatever you want
}
?>
jQuery.ajax({
url: "./_FILE_" // ./index.php for example
type: "POST",
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});

sending data through ajax to CI controller

I am new to ajax and CI .I want to send data and image through ajax . In my view i have 3 input fields and one image upload button .
var val1 = $("#val1"+id).val();
var val2 = $("#val2").val();
$.ajax({
type: "POST",
url: "page/save_data",
data: "{ val1 :'"+val1+"',val2:'"+val2+"}",
success: function(msg) {
alert(msg);
}
});
and in controller when i try this it shows me nothing
function save_data()
{
$val = $this->input->post('val1');
echo $val1;
}
In console it gives me nothing .
Try this :
$.ajax({
type: "POST",
url: "page/save_data",
data: { "val1 ":val1,"val2": val2},
success: function(msg) {
alert(msg);
}
});
Your ajax URL must be refer to your method name;
...
$.ajax({
type: "POST",
url: "page/save_data", //change this to your method;
...
should be like:
...
$.ajax({
type: "POST",
url: "page/save_iudata",//or whatever your method's name;
...
EDIT:
try this way:
function save_data(){
$val1 = $_REQUEST['val1'];
echo $val1;
}
Hello to send data via ajax is easy:
var data = {
val1: $('#val1').val(),
val2: $('#val2').val(),
};
$.ajax({
url: "<?php echo base_url().'page/save_data'; ?>",
dataType: 'json',
type: 'POST',
async : false,
data: data,
success: function(msg){
...
}
});
return false;
But to send an image you have to do some research...
here is a good tutorial

how can i send 2 id or more on this function by ajax

how can i send 2 id or more on this function by ajax
function advcust(id) {
$.ajax ({
url: "php_filter.php?cust=advcust",
type: "GET",
data: {CustStatus:id},
success: function(data){
$("#return").html(data)
}
})
}
here i can send only one id
but i need sent 2 or more id
and i have two input type to search two words
can i send 2 id on ajax
function advcust(id, anotherID)
{
$.ajax ({
url: "php_filter.php?cust=advcust",
type: "GET",
data: {CustStatus:id,SomeOverVar:anotherID},
success: function(data){
$("#return").html(data)
}
})
}
function advcust(ids) {
$.ajax ({
url: "php_filter.php?cust=advcust",
type: "GET",
data: {CustStatus: ids},
success: function(data){
$("#return").html(data);
}
});
}
var ids = [];
ids.push(id1);
ids.push(id2);
advcust(ids);
You can then access customer IDs in PHP as an array:
<?php
$customerIds = $_GET['CustStatus'];
foreach ($customerIds as $customerId) {
// do something...
}

Categories