redirecting is not working in ajax - ajax

This below script is in view/template folder.
<script>
$(document).ready(function(){
$("#change").click(function(){
$.ajax({
type: "POST",
url: "common/customer.php",
data: { oldemail: $("#oldemail").val(), newemail: $("#newemail").val() }
});
});
});
</script>
I want to send data in common/customer.php but it is not working. I
already used ../common/customer.php but same problem. What is the
solution?

Its a absolute path problem.
Your script is in view/template
If your path of javascript file is like something,
view/template/myjavascript.js
And if your path of customer.php file is
view/common/
Then you must switch your directory by ../
Use
../common/customer.php
Its a relative path of your file.

According to jQuery documentation, you must declare the data type:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
TRY...
url: "/common/customer.php", // note the leading slash

Sorry friends problem was in my html code. I used the type of input submit but when i changed it into button then it is working fine. Thanks to all of you.

Related

Ajax serialise - issue with data format

I have several inputs formatted with this jquery plugin here.
I use $.ajax to do my mysql insert:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
}),
I face an issue as my input values are formatted with the plugins and can't get into mysql db.
As an example:
Input value: $450,000.00 is not accepted.
Is there a way to unformat within the serialise function values that have a specific classes (like class="money")?
Thanks for your help!
I have tried the below code:
$.ajax({
type: 'GET',
url: 'xxx.php',
data: $('#new_form').serialize(),
dataType:"json",
beforeSend: function(){
$(".money").cleanVal();
},
<script>
function cleanVal(v) {
return v.replace(/^\,/,'');
};
</script>
the result of the insert in mysql is still 450 for 450,000.
Do you have an idea?
thanks
You can try using the plugin $.cleanVal() method to retrieve the unmasked type value of the corresponding HTML element, prior to your AJAX form submission. So something like this:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
beforeSend: function(){
$(".money").cleanVal();
}
}),
I couldn't make it work with beforehand. I found a solution which is to unmask values before calling ajax.
If anyone knows why it does not work with beforesend, thanks for letting me know.
cheers

Ajax data collect in code lines

I did some admin panel in wordpress sheet but i'm adding options and have everything in one line in the data, it's pain if I keep adding options, works that way but it looks messy.
example
$.ajax({
type: 'POST',
url: ajaxurl,
data: 'action=general_settings_action&zkr_logo='+zkrlogo+'&zkr_favicon='+zkrfavicon+'&zkr_background='+zkrbackground+'&zkr_linkcolor='+zkrlinkcolor+'&zkr_linkhover='+zkrlinkhover+'&zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
I would like to make some lines to that data field like for example
$.ajax({
type: 'POST',
url: ajaxurl,
data:
'action=general_settings_action&
zkr_logo='+zkrlogo+'&
zkr_favicon='+zkrfavicon+'&
zkr_background='+zkrbackground+'&
zkr_linkcolor='+zkrlinkcolor+'&
zkr_linkhover='+zkrlinkhover+'&
zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
But putting the code like that doesn't work I have tried with \n and some other stuff but still wont do the work.
I apreciate the help... Thanks
Try doing this:
Make a JSON data object that contains the parameters you want to send
var DATA = {
action:'general_settings_action',
zkr_logo:zkrlogo,
zkr_favicon:zkrfavicon,
zkr_background:zkrbackground,
zkr_linkcolor:zkrlinkcolor,
zkr_linkhover:zkrlinkhover,
zkr_colorbackground:zkrcolorbackground
}
The send the data in your AJAX request using the data field
$.ajax({
type: 'POST',
url: ajaxurl,
data: DATA,
success: function(data){
alert(data);
}});
I checked this out at: David Walsh's guide
You need to add
'zkr_logo=' + zkrlogo + '' +
instead of
zkr_logo='+zkrlogo+'&
then it will form one String

Ajax Get XML - Google Directions

I had the following script working two weeks ago in XAMPP, today it doesnt work.
var url = 'http://maps.googleapis.com/maps/api/directions/xml?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false';
$.ajax({
type: "GET",
url: url,
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find("route").each(function(){
var startaddresss = $(this).find('start_address').text();
alert(startaddresss);
});
}
});
If i save the XML to XAMPP and call it from AJAX it works fine. What changed in the last two weeks?
Thanks
Cant fix the issue. So i will use PHP to fetch the file and return JSON.

Symfony2 Ajax app_dev.php in url

Just starting out in Symfony2 and really loving it after being a long time ZF1 developer.
Started to add some Ajax functionality to a site tonight and am a bit confused about the following.
in my ajax call eg:
$.ajax({
url: '/app_dev.php/ajax/urlgetter',
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});
I had to add /app_dev.php to the url to make it work in dev environment. Is there not a better way of doing this? Does this mean when I change the project to a production environment I need to search and replace all instances of /app_dev.php?? Hopefully I have totally missed something simple.
I ended up using the jsrouting-bundle
Once installed I could simply do the following:
$.ajax({
url: Routing.generate('urlgetter'),
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});
where urlgetter is a route defined in routing.yml like:
urlgetter:
pattern: /ajax/urlgetter
defaults: { _controller: MyAjaxBundle:SomeController:urlgetter }
options:
expose: true
notice the expose: true option has to be set for the route to work with jsrouting-bundle
I guess this question is already kind of old but I came across the same problem.
Its not any "best practice" solution but here is what I do.
In twig you can use this {{ path('yourRouteName') }} thing in a perfect way. So in my twig file I have a structure like that:
...
<a href="{{ path('myRoute') }}"> //results in e.g http://localhost/app_dev.php/myRoute
<div id="clickMe">Click</div>
</a>
Now if someone clicks the div, I do the following in my .js file:
$('#clickMe').on('click', function(event) {
event.preventDefault(); //prevents the default a href behaviour
var url = $(this).closest('a').attr('href');
$.ajax({
url: url,
method: 'POST',
success: function(data) {
console.log('GENIUS!');
}
});
});
I know that this is not a solution for every situation where you want to trigger an Ajax request but I just leave this here and perhaps somebody thinks it's useful :)
Since this jQuery ajax function is placed on twig side and the url points to your application you can insert routing path
$.ajax({
url: '{{ path("your_ajax_routing") }}',
data: "url="+urlinput,
dataType: 'html',
timeout: 5000,
success: function(data, status){
// DO Stuff here
}
});

CodeIgniter - jQuery, JSON, simple example not working

Ok, Im a real newbie when it comes to ajax and json ... Im trying to figure it out in my codeigniter project.
Ive written something simple to start, just to bring up an alertbox, but it doesnt seem to be working, if someone could let me know where im going wrong, that would be grand.
In my view i have the following code.
$('.users').change(function(){
$.ajax({
type: "POST",
url: "/edituser/returndata",
data: {id: this.find(':selected').val()},
dataType: json,
success: function(data){
alert(data);
}
});
});
in the edituser/returndata controller, i just simply have the following
function returndata(){
echo $_POST['id'];
}
I know this will look pretty stupid to some people, but im still trying to figure it out, if someone could help :)
Cheers
----------------- UPDATED CODE BELOW
<script type="text/javascript" charset="utf-8">
$('#users').live('change', function(){
$.ajax({
type: "POST",
url: "/edituser/returndata",
data: {id: $(':selected', this).val()},
dataType: 'json',
success: function(data){
alert(data.id);
}
});
});
</script>
Controller code
function returndata()
{
$ID = $this->input->post('id'); // Use this instead of $_POST['id']
echo json_encode(array('id'=>$ID));
}
Your dataType should be:
dataType: 'json',
Your data should be:
data: {id: $(this).find(':selected').val()},
Inside of a event callback, this is a DOM element, so it needs to wrapped in $().
or:
data: {id: $(':selected', this).val()},
Which is the same as above, just less characters.
Also, in your PHP, you need to output JSON.
function returndata(){
$ID = $this->input->post('id'); // Use this instead of $_POST['id']
echo json_encode(array('id'=>$ID));
}
Then in your success function, you can do:
alert(data.id);
UPDATE
Disregard the answer. I thought the JSON was sent as a string, but it's not, as pointed out by Rocket. It is converted to url encoded value pairs. I'm leaving the answer up just in case someone thought the same thing as me....
The incoming JSON is not a request parameter, you need to read the body of the request
$json = json_decode(trim(file_get_contents('php://input'));
$id = $json->id;

Resources