How to call php class method using ajax - javascript

I'm new to ajax. I'm trying to call get_mother method through ajax in my form textbox change event. I want to show results in datalist. below is the code i have used.
class ChildApplication extends Application{
function __construct(){
$this->login_required();
}
function get_mother(){
$mother = $_POST['mother_name'];
$mother = '%'.$mother.'%';
$db=$this->get_dbo();
$sql = "SELECT * FROM tbl_mother WHERE `mother_fname` LIKE ? ";
$results = $db->load_result($sql,array($mother));
return $results;
}
function get_child($mother){
//statements
}
}
My script is:
$(document).ready(function(){
$("#mother_name").keyup(function(event){
event.preventDefault();
var mother = $("#mother_name").val();
$.ajax({
type: 'POST',
url: 'applications/child/child.php',
data: dataString,
dataType: 'json',
success: function(){
alert("pass");
},
error: function(){
alert("error");
}
});
});
});
none of alerts are displayed. please help me to solve the problem

I guess that "dataString" variable is not defined.
I think that you should replace the value of "data" like this:
data: {mother_name: mother},
Also make sure that the function get_mother() is called in "applications/child/child.php"
$ChildApplication = new ChildApplication;
$ChildApplication->get_mother();

Related

Can´t send post wirth javascript to another page

I´m trying to send the variable idValue to a class called cambio_prenda.php with the following code
function reply_click(clicked_id)
{
var idValue = clicked_id;
$.ajax({
type: "POST",
url: 'cambio_prenda.php',
data:{"cambio" :idValue},
success: function(response)
{
alert("exito");
}
});
}
This code works when i use it in the same class, but when I try to call it from the another class with this code it doesn´t work
<?php
if(isset($_POST['cambio'])){
$item = $POST['cambio'];
echo($item);
}
?>

Jquery Ajax call return Parsererror?

I have written Jquery Ajax call but i got Parsererror.. in following js code i have checked which select element is changed in dynamic_slct. and i have checked generic or Company and then call GnrtTemp() with pass selected Element value.. if I select generic my follwing Code(function GnrtTemp and ajax call) is working.. if i select company i got Error parsererror.. my doubt is same ajax call working Generic and is not working as company.. what is the problem? How to fix that problem i have attached my js code and also PHP code .. If i did any mistake pls correct me.. suggest to solution.?
JS
$('#dynmic_slct').on("change", "#master ,select[name='company'], select[name='generic']", function(element){
if(element.target.name == 'generic' || element.target.name == 'company') {
GnrtTemp(element.target.value);
}
});
function GnrtTemp(id){
$.ajax({
method: "POST",
url: "ajaxRequest.php",
dataType: "JSON",
data: {fn: "getTemp", id: id},
success: function(reqResult){},
complete: function (jqXHR, textStatus) {
alert(textStatus);
}
});
}
ajaxRequest.php
<?php
$finalRes = array();
if($_POST['fn'] == 'getTemp'){
$template_src = getTemp($_POST['id']);
$finalRes['result'] = $template_src;
echo json_encode($finalRes);
}
function getTemp($id){
$db = new DB();
$Query = "SELECT template_src
FROM master
WHERE refid =$id";
$qryRes = $db->query($Query);
return $qryRes;
}
?>

Yii2 Ajax Submission not working

Iam new to Yii2 and Ajax
I want to add multiple job for a work ,for that I pass id to WorkJobs Controller
This is my code for ajax submission
<?php
$this->registerJs(
'$("body").on("beforeSubmit", "form#w1", function() {
var form = $(this);
if (form.find(".has-error").length) {
return false;
}
$.ajax({
var jobid = "<?php echo $id;?>";
url: form.attr("work-jobs/create&id="+jobid),
type: "post",
data: form.serialize(),
success: function(errors) {
alert("sdfsdf");
// How to update form with error messages?
}
});
return false;
});'
);
?>
But it's not working ,I don't know what's wrong in my code ,please help ...........
change your code like below
<?php
$url=Yii::$app->urlManager->createUrl(['work-jobs/create','id'=>$id]);
$this->registerJs(
'$("body").on("beforeSubmit", "form#w1", function() {
var form = $(this);
if (form.find(".has-error").length) {
return false;
}
$.ajax({
url: "$url",
type: "post",
data: form.serialize(),
success: function(errors) {
alert("sdfsdf");
// How to update form with error messages?
}
});
return false;
});'
);
?>
Building off jithin's answer, make the following changes to your $.ajax() call
Make sure your URL is in quotes. It is a common mistake to forget to quote the URL when interspersing it with PHP. [jithin]
Unlike jithin's answer, you should do the following
instead of responding to the beforeSubmit event, handle the submit event. This would allow the Yii clientsoide validations do their job
the ajax.success callback takes data as the argument; not error, there's the ajax.failure callback for errors
Try using createAbsoluteUrl() in url like this:
url: "<?php echo Yii::app()->createAbsoluteUrl(\"work-jobs/create&id=\")"+jobid

Not sending data into controller with onclick function

I use this button in jason.
<button id="bid" type="button" class="button" onclick="connect()">Save</button>
It show altert when I use alert after document.getElementById. But it not works in ajax function. Here is my connect function
function connect()
{
var BID = document.getElementById('BID').value;
var REF = document.getElementById('ref_po').value;
var POU = document.getElementById('po_units').value;
//**alert(BID + REF + POU);**
var url ='<?php echo base_url()."index.php/main/transacIn/" ?>';
$.ajax({
type: "POST",
url: url,
data: 'BID='+BID +'&REF='+REF +'&POU='+POU,
success: function(data) {
//$("#bid").hide();
alert(BID);
}
});
}
alert(BID + REF + POU); this alert works but not works alert in success. And it don't sent any data into controller. Help me about it. Thanks in advance.
try this
function connect()
{
var BID = document.getElementById('BID').value;
var REF = document.getElementById('ref_po').value;
var POU = document.getElementById('po_units').value;
var site_url = document.getElementById('site_url').value;
var url = site_url+'main/transacIn';
$.ajax({
type: "POST",
url: url,
data: {
BID : BID ,
REF: REF,
POU:POU
},
success: function(data) {
//$("#bid").hide();
alert(BID);
},
error: function(err) {
console.log(err);
}
});
}
you should a hidden field in your corresponding page <input id="site_url" type="hidden" value="<?php echo site_url(); ?>"/>
You cannot inject the php syntax into javascript file. Either you define the script in codeigniter template file as
<script type="text/javascript">
// Your javascript function
</script>
...or you can define a javascript variable in the template file which actually get the path to your controller:
var url = <?php echo base_url()."index.php/main/transacIn/" ?>';
Then on your javascript file on ajax post you are pointing to the variable defined in the template file:
$.ajax({
type: "POST",
url: url,
data: 'BID='+BID +'&REF='+REF +'&POU='+POU,
success: function(data) {
//$("#bid").hide();
alert(BID);
}
});
Hope you got the idea.
Anyway you need to be careful defining global variables. It's a good practice to guard the variables by using namespaces.
Try this solution. It will work:
$.post(url , {BID: BID, REF: REF, POU:POU },
function(data){
alert(BID);
});

Javascript passing variable issue not returning

Hi All I have the following code to pass a JS variable using AJAX as seen below:
function buttonCallback(obj){
var id = $(obj).attr('id');
$.ajax({
type: "POST",
url: "/project/main/passid",
data: { 'id': id },
success: function(msg){
window.alert(msg);
}
});
}
if I put an alert box in I can see the object id is successfully getting grabbed. however if in php I want to simply return the variable - I am getting a null has anyone got any ideas:
heres my PHP function (I am using Codeigniter):
public function passid(){
$courseId = $this->input->post('id');
echo $courseId;
}
EDIT: the success alert box appears - but appears blank and that is my issue I am hoping to see the ID
1. Can you make sure id equals something by doing this:
function buttonCallback(obj){
var id = $(obj).attr('id');
alert( id ); // What does it alert?
$.ajax({
type: "POST",
url: "/project/main/passid",
dataType: "json",
data: { 'id': id },
success: function(msg){
window.alert(msg.id);
}
});
}
2. Your javascript looks good... Can you try this instead to see if it works:
JS
function buttonCallback(obj){
var id = $(obj).attr('id');
$.ajax({
type: "POST",
url: "/project/main/passid",
dataType: "json",
data: { 'id': id },
success: function(msg){
window.alert(msg.id);
}
});
}
PHP
public function passid(){
$courseId = $this->input->post('id');
$response = array( "id" => $courseId );
header('Content-Type: application/json');
echo json_encode( $response );
}
3. If this does not work, can you try and rename from id to something like poopoo, just to make sure id is not taken and being weird?
4. Can you check what the network response is of your ajax request - Go to developer toolbar and goto network section, make sure you hit the record/play button. then send your request off. When you see your request come up in the network list, check the "details" of it and goto response.

Categories

Resources