Phalcon jquery-ajax on button click - php

I am warking in phalcon framework, and i am trying to call controller's method to get simple string using jquery-ajax. When i placed my ajax call inside $(document).ready(function() ajax call worked, but when i placed same code inside $('#dugme').click(function() ajax call reported error. I am confused. Here is my view code:
<script type="text/javascript">
$(document).ready(function(){
//alert($("#nesto").val());
//atr = $(".klasa").attr("id");
//alert("Id je: " + atr)
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
$('#dugme').click(function(){
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
}) ;
});
</script>
<h2>Basic example</h2>
<?php echo Tag::form("xml/pretraga"); ?>
<p>
<label for="name">Title</label>
<?php $opt = array('title', 'id'=>'nesto', 'size'=>'10');
$buttopt = array('Show', 'id'=>'dugme','class'=>'klasa');
?>
<?php echo Tag::textField($opt) ?>
</p>
<p>
<?php echo Tag::SubmitButton($buttopt) ?>
</p>
</form>
and here is my action code:
public function posaljiAction(){
$this->view->disable();
$data = "My name is Nedimo";
echo json_encode($data);
}
Please, can anyone tell me what is wrong in my code.

Related

define a string as POST request using AJAX in the same page onclick

for the PHP is just simple check:
All in the same page named **
page.php
if(isset($_POST['XYZ']){
echo "WORKING";
}
then for my HTML:
<h1 id='XYZ'>CLICK ME</h1>
now at the same page i'm trying to do the AJAX POST request like this
$('#XYZ').click(function(){
var XYZ = 'XYZ';
$.post('page.php',{
XYZ: XYZ
})
})
and the request didn't work, How do i just pass the $_POST data? I removed success function since i didn't think it is useful in this case.
What i want is when i click on the <h1> the echo appears.
try this:
var my_string = 'xyz';
$.ajax({
url: 'ajax.php',
type: 'post',
data: { "action":my_string},
dataType: "json",
success: function(response) {
if(response['state'] == 'ok'){
console.log("ok");
}
}
});
ajax.php
<?php echo $_POST['action']; ?>
<head>
<script>
var my_string = 'xyz';
$.ajax({
method: 'POST',
url: './giveposts',
dataType: "text",
contentType: "application/json; charset=utf-8",
data:my_string,
success: function(data) {
{
$("#test").html(data);
}
}
});
</head>
</script>
<body>
<div id="test>
/*echo
</div>
</body>

json ajax update HTML element not working

I am trying to get a HTML element to update from an ajax quire to a PHP file but it's not working.
My code is below
<html>
<head>
<script src="jquery-3.1.0.min.js"></script>
</head>
<body>
<script>
var json = (function () {
var json = null;
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
json = data.test;
$('#demo').text(json.test1);
}
});
return json;
})();
</script>
<p id="demo"></p>
</body>
</html>
PHP code
<?php
header("Content-Type: application/json");
$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';
echo json_encode($test);
//echo nothing after this //not even html
?>
can someone please help, thank you
Your php script sending a JSON object and you can access its property like this:
$.ajax({
url: "test.php",
dataType: "json", //the return type data is jsonn
success: function(data){ // <--- (data) is in json format
$('#demo').text(data.test1);
}
});
the Javascript is the problem. You should bind the function that makes the AJAX query to a DOM event, such as a button click.
<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
$.ajax({type: "POST",
url: "test.php",
dataType: "json",
data: { name: $("#name").val()},
success:function(data){
$("#demo").text(data.test1);
}
});
});
});
</script>
<input type="text" id="name" placeholder="Enter your name">
<button id="submit">Submit</button>
<p id="demo"></p>
On PHP side, you can read the input:
<?php
header("Content-Type: application/json");
$test = [
'test1'=>1, 'test2'=>2, 'test3'=>3, 'name'=>$_POST['name']
];
echo json_encode($test);
exit;

How to check php is loaded when using AJAX

I use AJAX, and this work without problems, like this:
$('#main .left').load('top.php');
or this:
$.ajax({
type: "POST",
url: '/ajax_main.php',
data: "i=ajax_call",
success: function(msg){
console.log('ok');
}
});
// php side reprorts $_GET['i'] = "ajax_call";
When I set:
function periodicMethod(){
$.ajax({
type: "POST",
url: 'ajax_main.php',
data: "i=ajax_call",
success: function(msg) {
console.log('ok');
}
});
}
setInterval(periodicMethod, 1000);
The method is called and console.log reports "ok" any second. This looks like 'ajax_main.php' is loaded. But in the PHP file:
edit
echo '<script>console.log("php");</script>';
Does not give any results. The path in URL is ok. When I change it or remove 'ajax_main.php' from server, error is reported. What may be wrong?
edit
another version of php:
<script>
function ppp()
{ console.log('sss');
}
</script>
<?
echo '<script>ppp();</script>';
?>
is not working to
try this code
<script>
function periodicMethod(){
$.ajax({type: "POST",url: 'ajax_main.php',data: "i=ajax_call",
success: function(msg){ console.log('ok');
jQuery("#demo_data").html(msg);
}});}
setInterval(periodicMethod, 1000);
</script>
<html>
<body>
<form action="" method="post">
<input type="hidden" name="demo_data" id="demo_data"/>
</form>
</body>
</html>

php variable to ajax data

I have edited my code and it goes something like this and it is not working.. Please help me..
<?php
$variable = "krishna";
?>
<script>
$.ajax({
type:"POST",
url:"ajax.php",
data:{
variable:<?php echo $variable; ?>
},
success:function(msg){
$("#val").html(msg);
}
});
</script>
<div id="val"></div>
ajax.php
<?php
echo $_POST['variable'];
?>
thank you all
Try this
<script type="text/javascript">
$.ajax({
type: "POST",
url: "",
data: 'var=<?php echo $variable;?>',
success: function(){
}
});
</script>
<script>
$.ajax({
type:"POST",
url:"",
data:{data:'<?php echo $data; ?>',data1:'<?php echo $data1; ?>'}
success: function(data)
{ }
});
</script>
You can add any number of varibles using data{data1:data1, data2:data2, data3:data3} and it's stand like {variablename:value}
Use an echo statement inline with the javascript. Because PHP executes on the server all the PHP processing will be done by the time the javascript runs.
<?php
$variable = "php";
?>
<script>
$.ajax({
type:"POST",
url:"",
data:{
variable:"<?php echo $variable; ?>"
},
success:
});
</script>

json jquery post request

<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var content = $('#content').html();
var data = {"content":content};
$.ajax({
type: "POST",
dataType: "json",
url: "ajax.php",
data: {content:content},
success function (data) {
alert('Hello!');
}
});
});
});
</script>
<div id="content"><?php echo $content; ?></div>
ajax.php
echo json_encode($_POST['content']); ?>
Nothing happens... WhatI really want to achieve is to get that alert box and get the return data, but I am lost since I don't get any errors or nothing.
You miss " : " after success
<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var content = $('#content').html();
var data = {"content":content};
$.ajax({
type: "POST",
dataType: "json",
url: "ajax.php",
data: {content:content},
success: function (data) {
alert('Hello!');
}
});
});
});
</script>
<div id="content"><?php echo $content; ?></div>
As #sofl said, if you change it to success:function (data) { it will work!
Just remember that the $("a") from $("a").click(function() { called when click in a link tag like <a href"">.
If you are using an input ou button with a class="a" you should change the code to $(".a").click(function() {
(just add a . before a)
PS: If you're using a link, you should set the href="" to href="#" to work.

Categories