I am Using an HTML dropdown on a page, my requirement is to when i select any value from dropdown, it want to be store in an PHP variable on the same page without submitting form or without refreshing the page.
my code is:
Script:
<script type="text/javascript">
$("#bank_name").change(function(){
var data=$(this).val();
alert(data);
$.ajax({
url:"transaction.php",
type:"POST",
data: ({"bank_code": data}),
sucess: function(response){
console.log("Success");
alert(data);
},
error: function(response){
console.log("Error");
alert(data);
}
});
<script>
PHP Code:
$bank_code=$_POST["bank_code"];
echo "Bank Code is:",$bank_code;
it gets the value of selected option and also alerts the value but after alert it doesn't echo the value in PHP.
You have type error in success ,You should remove those () brackets in data field to access $_POST in php like below,
<script type="text/javascript">
$("#bank_name").change(function(){
var data=$(this).val();
alert(data);
$.ajax({
url:"transaction.php",
type:"POST",
data: {bank_code: data),
success: function(response){
console.log("Success");
alert(response);
},
error: function(response){
console.log("Error");
alert(data);
}
});
<script>
The echo will be received as response in the success callback
$("#bank_name").change(function() {
var data = $(this).val();
alert(data);
$.ajax({
....,
success: function(response) {
console.log("Success");
// alert response instead
alert(response);
},
......
});
Related
I use web services, i try this code. i want to select and show my php page , select 1.string or 2.string
ajax.php
$bb=json_decode($result,true);
$a= strlen($bb[$user_id]['1']);
if ($a!=0)
{
$this->data[$bb];
echo "<br>User ID===> ".$bb[$user_id]['1'];
echo "<br>name===> ".$bb[$user_id]['2'];
echo "<br>surname===> ".$bb[$user_id]['3'];
echo "<br>etc..===> ".$bb[$user_id]['4'];
}
x.php Js codes
$.ajax({
type:'post',
data:{user:user,user_id:user_id},
url:'<?php echo site_url('ajax/hello');?>',
success: function(result){
$('#htmlname').html(result);
}
});
});
});
Return the array as JSON. To do this, set the dataType to JSON in your ajax call: dataType: "json"
You can then access each element of the array in the success callback:
success: function (result) {
var item1 = result[0];
var item3 = result[2];
}
I have the following HTML fields being created inside a PHP look
<td><input type=\"checkbox\" name=\"investigator_id[]\" id=\"investigator_id\" value=\"$name_degree[$i]\">
<td><input type=text name=\"inv_rank[]\" id=inv_rank maxlength=\"2\" size=\"2\"></td>
<td><textarea name=\"inv_comm[]\" id=inv_comm rows=2 cols=20></textarea></td>
I am trying to save the data in these fields by calling a jquery function based on clicking on this button
Here is the script that is being called. I know that the js is being called because the "alert("now")" is poping up, but the dataString is not being populated correctly. I tested this on http://jsfiddle.net/ and it worked fine, but won't work on my site.
<script>
$(document).ready(function() {
$("#submit").click(function() {
alert("now");
var dataString = $("'[name=\"investigator_id\[\]\"]', '[name=\"inv_rank\[\]\"]','[name=\"inv_comm\[\]\"]'").serialize();
alert("ee"+dataString);
$.ajax({
type: "POST",
url: "save_data.php",
dataType: "json",
data: dataString,
success: function(data){
alert("sure"+data);
$("#myResponse").html(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("There was an error.");
}
});
});
});
</script>
Try this with the help of FormID like this:
<form method="post" id="yourFromID">
//Your form fields.
</form>
JS Code:
$("#yourFromID").submit(function (e){
e.preventDefault();
var dataString = $(this).serialize();
// then you can do ajax call, like this
$.ajax({
url: 'site.com',
data: dataString,
methodL 'post',
success: function(){...}
})
return false;
});
I am trying to alert anything inside the success ajax call, but it's not even alerting, I have checked my chrome insoector network and I am getting a 200 OK...
Here's my ajax call:#
$("#content").on("submit", "#add_qualification_form", function() {
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
headers: {'X-Requested-With': 'XMLHttpRequest'},
dataType: 'json',
data: $(this).serialize(),
success: function(data) {
alert(data);
if(data.success == "false"){
$.notify(data.error, "error");
}else{
$.notify(data.success, "success");
$('#add_qualification').text("+ <?php echo System::translate("Add qualification"); ?>");
$(".add_qualification_json").slideUp("slow");
$("#content").load("<?php echo Config::get('URL'); ?>qualification/qualifications" + " #inner_main_content");
$("html, body").animate({ scrollTop: 0 }, "slow");
}
}
});
return false; // important: prevent the form from submitting
});
And this is my response when directly viewing the page:
{"success":false,"error":"All inputs must be complete. Try again"}
But my alert is not working, nothing is working inside the success: function
The repsonse is a json, hence why I have set the dataType,
As stated by Guruprassad in the comments, I had to check the errors in the following function
error: function(jqXHR, responseText,status){
console.log(jqXHR.responseText)
}
This helped check for server side errors rather than other errors.
I'm using onload() and ajax to get the array from php, but it didnt work. The html page should be able to get the array from n1.php and alert("GOOD"), but it's not giving any response, not even alerting GOOD or BAD so i really dont know what's wrong with the code. How can I fix this??
n1.html:
<!DOCTYPE html>
<html>
<body onload="getArr();">
here
</body>
<script type="text/javascript">
function getArr(){
alert('return sent');
$.ajax({
url: "n1.php",
dataType: 'json',
success: function(json_data){
var data_array = $.parseJSON(json_data);
var rec = data_array[0];
alert("GOOD");
},
error: function() {
alert("BAD");
}
});
}
</script></html>
n1.php:
<?php
$output = array("cat","dog");
echo json_encode($output);
?>
The request must contains the type of request. Also the dataType refers on data you are going to send,as long as you don't send any data, it does not need here.
Try this:
$.ajax({
url: "n1.php",
type: "GET",
success: function(json_data){
var data_array = $.parseJSON(json_data);
var rec = data_array[0];
alert("GOOD");
},
error: function() {
alert("BAD");
}
});
Try this
$.ajax({
url: "n1.php",
dataType: 'json',
success: function(json_data){
var data_array = json_data; // Do not parse json_data because dataType is 'json'
var rec = data_array[0];
alert("GOOD");
},
error: function() {
alert("BAD");
}
});
Now, two things to note here:
You have not passed HTTP Method in the ajax but by default it is GET as mentioned here in jQuery AJAX docs. Please pass appropriate method type if it is not GET.
Since you have sent dataType as 'json', you need not parse json received in the response in success handler.
I want to send information to the screen id that information sent to it by the echo. I get this interaction takes place via ajax would like to receive information from the entire screen again displays. Please help
$(document).ready(function(){
$('#button').click(function(){
var id = $(this).attr('id_1');
$.ajax({
url:'get.php',
type: 'get',
data:{id:id},
success: function(data){
//alert(data);
$('#result').html(data);
}
});
});
});
<body>
<?php if(isset($_GET['id'])){echo $_GET['id'];}?>
test
<div id="result"></div>
after result
20
<a>test</a>****------------------------------>Is repeated
<a>test</a>
but i want just this result
20
Change you type as GET in AJAX
$('#result').empty();
$.ajax({
url:'get.php?id='+ id,
type: "GET",
//data:{id:id},
success: function(data){
//alert(data);
$('#result').html(data);
}
});
});
Try this. It will displays the output as you said.
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(){
var id = $(this).attr('id_1');
$.ajax({
url:'get.php',
type: "get",
data:{id:id},
success: function(data){
$('#result').html(data);
}
});
});
});
</script>
<div id="result">
test</div>
This is get.php
<?php if(isset($_GET['id'])){echo 'hi';}
?>
if you want exit:20 as output, then change get.php as
<?php if(isset($_GET['id'])){echo 'exit : '.$_GET['id'];}
?>