Dynamic Javascript Ajax + Php - php

I'm not sure this question has the best of titles but I'm not sure what else to call it so sorry for that.
I'm using ajax to pull in content for a div on my website (after an option is selected). The content is a form generated by a PHP script. When the form is submitted a JavaScript function should be called but I'm just getting an error that says the function can't be found.
The JavaScript is pulled in via ajax with the form and I can't really change that as it needs to change demanding on the option selected.
My question is should this work? if not I'll just have to re think the way I'm doing it, just wanted to check if it wasn't working because it never will or if I'm doing something wrong.
I would show the code but it's very long.
Thanks in advance!
Edit: thanks for all the comments ect, apologies for not including the code before here it is.
function select(id){
$.ajax({
url: 'select/'+id,
type: 'GET',
dataType: 'html',
success: function(msg) {
$('.product_details').html(msg);
return false;
}
});
}

Are you using a javascript library?
With jQuery specify a data type of html and make sure the script tags are before the HTML in the response
$.ajax({
url: "something.php",
dataType: "html",
success: function(data, text, request) {
...
}
});
in mootools...
var myRequest = new Request({
url: "something.php",
evalScripts: true,
onSuccess: function(responseText, responseXML){
....
}
});
myRequest.send();
Now your passed tags will be evaluated and available to the DOM

Related

How to properly pass a jquery value to php using ajax?

I want to pass a value from my jquery code to a php variable after a user inputs through a textbox, but I don't receive any value at all in the PHP side through POST.I'm using .change and .post, does anyone here knows how to do it properly?
here's my code:
$(document).ready(function(){
$("#packagename").change(function(){
var packagename = $('#packagename').val();
var url = '{{url}}'; //localhost/test/test.php
$.ajax({
type: 'post',
url: url,
dataType:html,
data:{'val':packagename},
});
});
});
try it
$(document).ready(function(){
$("#packagename").change(function(){
var packagename = $('#packagename').val();
var url = '{{url}}'; //localhost/test/test.php
$.ajax({
type: 'post',
url: url,
dataType:text,
data:{'val':packagename},
success:function(result){
alert(result);
}
});
});
});
The only problem that I can see offhand is that the html in dataType:html, needs to have quotes around it like this: dataType: 'html',. Otherwise your code will look for the variable html, not find it, and throw an error.
http://api.jquery.com/jQuery.post/
dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
change dataType: html, to dataType: 'html',
Have you tried just putting the url into the url field instead of whatever that object is you are trying to use?
I will assume you are not trying to ajax to the page you are currently on and expecting the php variable to display on the current page.

Ajax load duplicates entire page

I am trying to refresh a div with jquery load(); but the load displays the correct information but duplicates entire parts of the page that arent in the div
$.ajax({
type: 'POST',
url: $(this).attr('action'),
cache: false,
data: $("#uses_form").serializeArray(),
success: function(data)
{
$('#uses_form_div').load('#uses_form_div');
}
});
return false; });
i think you are having a misunderstanding..
if you want to load an external url into the div block
$('#uses_form_div').load("./a.file");
will do it. see the api docs.
Or if you are trying to load the ajax response of the $.ajax call into the div, it should be
$.ajax({
type: 'POST',
url: $(this).attr('action'),
cache: false,
data: $("#uses_form").serializeArray(),
success: function(data)
{
$('#uses_form_div').html(data); // see here
}
});
UPDATED according to comments below:
if the case of an included page to be refreshed. I have two ways to recommend.
make the included file as a separate url and load it initially. so instead of include you will be loading it via jquery as the page loads by a jquery load call. and when you want to refresh it you can do $('#uses_form_div').load("./a.file");
you can put it as include it self and when you need to update, make an ajx request get the data back. Here you have 2 choice. You can build the dom at server and give html as ajax response and simply $("#uses_form_div").html(data) or get the response as json
and build your dom at client side and load it via same $("#uses_form_div").html(data).
I also had the same problem but finally found the answer
<script type="text/javascript">
function recp() {
setInterval(function()
{
$("#result").load(location.href+ ' #my');
});
}
</script>
<div id="result">
<div id="my"><?php echo date('a:i:s'); ?></div>
</div>

function Mootools & Ajax to POST date to form

My first question :)
I am using Mootools and i have a JavaScript function, my issue is how to post data to form without refreshing the page.
I think that my code is not working with Mootools, Please check and advise
<script type="text/javascript">
function (PostData) {
$.ajax({
type: "POST",
url: "form.php",
data: { userid: 1111, test: 'test'}
})
}
</script>
Thanks
As it was already stated. the $.ajax method is a Jquery method. If you want to do it in mootools you should use the Request Class.
Here's the Mootools request documentation in case you want (and should) dig a little deeper.
Here's what you want to achieve using Mootools
var request = new Request({
url: 'form.php',
method: 'post',
data: {
userId:1111,
test: 'test'
},
onSuccess: function(response){ Whatever you want to do}
}
request.send();
You could also use the Request.JSON object, which automatically decodes the response into a Json object.

Send AJAX request by clicking a link without redirecting user

I have a web application which features a bunch of different items, which are generated from a MySQL table. As users scroll through it, I want them to be able to click a link next to the item which will insert the request into a MySQL database. Normally, I’d do this by creating a PHP page (which I will do anyways) that grabs the item name & user id from the URI using the $_GET method & inserts it into the table. However, in this case, I don’t want the users to be redirected away from wherever they are. I just want the link to send off the request, and maybe display a small message after it is successful.
I figured jQuery/AJAX would be best for this, but as I’m not too familiar with it, I’m not sure what to do. Any tips are appreciated!
You have to do something like
$('.classofyourlink').click(function(e){
e.preventDefault();//in this way you have no redirect
$.post(...);//Make the ajax call
});
in this way the user makes an ajax call by clicking a link without redirecting. Here are the docs for $.post
EDIT - to pass the value to jQuery in your case you should do something like
$('.order_this').click(function(e){
e.preventDefault();//in this way you have no redirect
var valueToPass = $(this).text();
var url = "url/to/post/";
$.post(url, { data: valueToPass }, function(data){...} );//Make the ajax call
});
HTML
<a id="aDelete" href="mypage.php">Delete</a>
Script
$(function(){
$("#aDelete").click(function(){
$.post("ajaxserverpage.php?data1=your_data_to_pass&data2=second_value",function(data){
//do something with the response which is available in the "data" variable
});
});
return false;
});
See http://api.jquery.com/jQuery.ajax/
$('#my-link').click(function(){
$.ajax({
url: "mypage.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
return false;
});
$('.classOfYourLinkToBecliked').click(function(){
$.ajax({
type:'GET',
'url':'yoururl',
data: {yourdata},
processData: false,
contentType: false,
cache: false,
dataType: 'json',
success: function(response){
alert(response);
}
});
});

Problem with using Jquery.ajax over .load on Zend

Right now, what i'm trying to do is to replace a label on the front page with a block of html. Right now, the page basically has:
<label id="replace"></label>
the js currently has:
$(document).ready(function(){
$("#replace").load('/test');
});
the Zend class function has:
public function indexAction(){
$this->_helper->layout()->disableLayout();
$this->_view->message = "This is from TestController index";
}
and finally the index.phtml template simply has:
<?php echo $this->message;?>
Right now, I want to change the code around so that instead of just replacing that label with the same message, it would do a POST where the function will pull out a parameter, do something (like for instance, go to the database and pull something out with the POST parameter) and then return the message.
I've tried editing the js so that it would look like:
$.post('/test', {param : "test_param"},
function(data) {$("#replace").html(data);});
or
$.ajax({
type: 'POST',
url: '/test',
data: "{param:test_param}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {$("#replace").html(data);}
});
and neither worked. I took a step back and tried to replicate the .load functionality and do:
$.ajax({
url: '/test',
success: function(data) {
$('#replace').html(data);
alert('Load was performed.');
}
});
and it doesn't work either.
Anyone have any tips on how to go about doing this?
Are you sure that you can call /test directly with out using ajax? Also see what is the absolute url you are calling to. Try adding error callback and see it works, it will give you the error response which will help you fix the problem.
The original .post code you have looks right
$.post('/test', { param: "test_param" }, function(data) {
$('#replace').html(data);
});
You can also try looking at your browsers JavaScript console to see if any errors are being reported.
Is your controller set to auto JSON encode the view parameters?
Either way I think your supposed to access it like
{ success: function(data) {$("#replace").html(data.message); }

Categories