i am using ajax to populate a drop down but the call is not going to the server. getting the following error in fire bug
POST 0
status 404 not found
my code is:
function selectChildCategory(parent,child){
var url = "<?php echo url::site('admin/video/showSubCategory/')?>";
if(parent != "")
{
if(child != 0){
url = url+parent+"/"+child;
}else{
url = url+parent+"/"+0;
}
$.ajax({
url: url,
type:"POST",
success: function(select)
{
//alert(select);
$("#sub_category").html(select);
}
});
}
}
the parammeters are showing correct values....but call is not going to the server. The URL is correct
please advise.
404 Error code means that the page your are trying to call does not exists.
You are buildng a HTTP path using your parent and child variables but if the formed url does not exists (or is not captured by any mod_rewrite) a 404 error is shown.
For example, for parent "0" and child "0" the url /admin/video/showSubCategory/0/0 exists?
Also if you are using something like mod_rewrite is really correctly configured?
Try first calling the URL manually to check if the url generated by javascript really exists.
Have you checked that the URL is available via a POST request?
Related
I am trying to flash a success message when data store successfully. My database code and every other thing working fine but when I give $_SESSION success message it always come under navigation.
1) I want to know is there any way in php to set margin of $_SESSION['success_flash'] message
2) is there any way like /n/n type so that my flash message will show two line below
3)
My real code is:
$_SESSION['success_flash'] = '\n\n<span style="color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';
I tried /n/n but not working:
$_SESSION['success_flash'] = '\n\n<span style="color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';
I tried style="margin-top:200px;" but not working:
$_SESSION['success_flash'] = '<span style="margin-top:200px;color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>';
Kindly check the image below only green background is showing under the navigation bar and the text message "Data saved successfully!" not showing because it is hidden under the navigation.
One more thing I am using below code after the flash message and my code end.
echo "<script type='text/javascript'> document.location = 'index.php'; </script>";
Any idea or suggestion would be helpful.
Thanks you.
I am getting data and passing the data to database successfully. I just want to reload my page after php call and show sucess flash to the user.
My Ajax Call: top my php page.
<script>
function createD () {
var data = {
'name' : jQuery('#name').val(),
'phone' : jQuery('#phone').val(),
};
//Ajax call Start Here
jQuery.ajax({
url : '/mycodpage/includes/codfile/product.php',
method : 'POST',
data : data,
success : function(data){
if (data != 'passed') {
// This will show error
jQuery('#modal_errors_1').html(data);
}
if (data == 'passed') {
//clear the errors if any
jQuery('#modal_errors_1').html("");
location.reload();
}
},
error : function (){alert("Something went wrong.");},
}); //Ajax call End Here
}
} // Function End
</script>
PHP once loaded it does not change on the page, it is not dynamic. The way you do this is rather weird, but can be acomplished if you will set $_SESSION['success_flash'] just right after saving the data in the database, and reaload the page.
In my opinion the best way is to use ajax here, and the ajax response can change the container under the menu to "success".
But I don't know how do you send your data, is it by POST form or something?
Add display:inline-block; or display:block; in the styles of the span.
As long as dom elements behaviour can depend on outer dom objects (and you do not specify them): if it still does not work try using padding instead of margin, if it still does not work place two span tags one inside other both with inline-block.
All that stuff using $_SESSION is kinda weird, however XD... but from your description I assume that it shows already the span, but without the margin... but you should not use $_SESSION in that way.
OK, now we are getting somewhere. But now location.reload is unnecessary, because if you will receive 'passed' answer, you can set the communicate without reloading (and without using any code in session variable, get rid of $_SESSION['success_flash']), like below.
Please note that I do not know what is the CSS class or ID of your green DIV (if any), so for this specific example I will assume, that it has a class ".msg-response"
<script>
function createD () {
var data = {
'name' : jQuery('#name').val(),
'phone' : jQuery('#phone').val(),
};
//Ajax call Start Here
jQuery.ajax({
url : '/mycodpage/includes/codfile/product.php',
method : 'POST',
data : data,
success : function(data){
if (data != 'passed') {
// This will show error
jQuery('#modal_errors_1').html(data);
}
if (data == 'passed') {
//clear the errors if any
jQuery('#modal_errors_1').html("");
// this new line will change the content of your message div
$('div.msg-response').html('<span style="margin-top:200px;color:#FFFFFF;text-align:center;">Data Saved Successfully!</span>');
}
},
error : function (){alert("Something went wrong.");},
}); //Ajax call End Here
}
} // Function End
</script>
there is also a security risk here: "if (data != 'passed')" - you will echo PHP errors (path, filename, variables, class, code row etc.) when something unpredictionary will happen, unless you will turn display_errors off on your server (display errors should be enabled only in development mode, that's a good practice).
To avoid it you can use JSON response here, handle the "error" status, "success" status, and do nothing if JSON response is different from "success" or "error".
I am using local host to host this test site and in one of my java-script files i created an ajax request to a php script
if(hIF == "true"){
$.ajax({
type: "POST",
url: "log_in/login.php",
data: {name: userName, pwd: password},
success: function(response){
if(response=="true")
{
changeColor('#add_err', 'rColWet', 'gColor');
$("#add_err").text("Login Successfull");
$('#clEx').delay("slow").click();
//window.location = "profile/dashboard.php";
}
else if(response=="false")
{
changeColor('#add_err', 'gColor', 'rColWet');
$("#add_err").text("Login Failed");
}else {
$("#add_err").text(" Abnormal response from server");
alert(response);
}
}
});
and in a section of my PHP script, i have a call to a function that is expected to redirect the user to a different page if the preceding condition is true.
if($num_row >=1){
move_to();
}
else{
echo 'false';
}
Here is the function which is located at the top of the php script.
function move_to(){
header('location:..//profile//dashboard.php?');
exit;
}
Everything seems to be in correct order, but when i run the code it does not redirect me to the page specified in the move_to function, but instead sends the content of the php script (i.e that is the file i expect to be redirected to) back to the ajax call.
I would be comfortable with using "window.location = "" for redirecting to a different page, as this worked perfectly when i used it, but i would like to see this header method for redirecting work (i.e using the header function to redirect). I also want to know why google does not recommend using "window.location" for redirecting and what are the downsides to using this method (i.e using window.location for redirecting). Thank youuu :)
Why are you creating a function for redirecting....Anyway, you can still use window.location from php as per code below
echo "<script>window.location='..//profile//dashboard.php'</script>";
make sure that the URL is correct otherwise use the full URL link Path for testing Eg. http://localhost/profile//dashboard.php
In a nutshell try this
if($num_row >=1){
move_to();
header("Location:../profile//dashboard.php");
//or
echo "<script>window.location='..//profile//dashboard.php'</script>";
}
else{
echo 'false';
}
Can anyone explain why I seem to be getting a double success response from this AJAX request using Bootstrap modals?
I am getting back testtest instead of test.
I'm pretty sure from looking at the console there is only 1 request being made and I've checked the scripts are only being loaded once.
Button triggers script:
<i class="fas fa-pencil-alt">Notes
Javascript(A bit convoluted from trying previous fixes found on S.O.)
function openNotesModal() {
$('#notesModal').modal({
keyboard: false,
backdrop: 'static'
});
}
$('.openNotesModal').click(function() {
openNotesModal();
});
$('#notesModal').on('shown.bs.modal', function(e) {
jQuery.ajax({
url: $('.openNotesModal').attr('data-note'),
success: function(response) {
$('#notesModal #theNote').val(response);
console.log(response);
}
});
});
AJAX request routed to this method
public function action_thisNote($ra,$qID,$bID = null)
{
echo "test";
}
Should return 'test' but returns 'testtest' instead
Modifying the method my AJAX call was routed to has fixed this. The issue is actually related to Concrete5 CMS which I'm building on top of.
Controller methods prefixed action_ are routed via urls for use in form submission. I think they auto-redirect after a form is submitted and this was causing problems with my AJAX request in the bootstrap modal. Calling exit(); function after returning my value stops the redirect and any other onward processing by the CMS.
Thanks to #epascarello in the comments above for leading me to the fix by suggesting the obvious of checking the URL outside of AJAX request helping to isolate the issue.
Fixed PHP function/method:
public function action_thisNote($ra,$qID,$bID = null)
{
echo 'test';
exit();
}
Okay, so, I have a PHP and JS-based webapp that does mostly what it's supposed to do. This being said, before I show any code, bear in mind a few things.
1.)The top two location headers inside the 'if' loops work as intended.
2.)In the development tools in Chrome, the location is in the header for this request.
PasteBin to my code here.
In my JQuery (defined in html head), we see a button name "subAll2", which is supposed to post the intended data, and do a redirect using aforementioned JQuery. The other two redirects work via intended forms. Why does this one not work?
$('#subAll2').click(function() {
var action = $('#frmUser').attr('action');
$.ajax({
url : action,
type : 'POST',
data : $('#frmUser,
#frmUser2').serialize()+"&butSubmitAll=submitAll",
success : function() {
window.location.replace(action);
}
});
return false;
});
Obligatory codeblock for requirements. Please check the PasteBin.
I found the answer. In the JS, replace the success function with
success : function(data) {
if (data == "success")
windows.location = "list_user.php";
else
alert("Form didn't go through.");
}
And instead of
header("Location:list_user.php");
Replace with
echo "success";
I'm using an ajax post request to authenticate users, this script replies with a full webpage. Within the response if successful there is a div called #logged.
What I would like to do, is check if the requested result contains logged and then replace content within the corresponding #primary-content div. How can I do it?
I want to check if the response contains a div called logged, then get the content of a div called 'primary-content' contained in the response string.
Pretty simple really... Once your ajax object readyState has reached 4.
Just do this... presuming your ajax or data object is called ajax.
var logged = $("#logged", $(ajax.responseText)); //grab the logged element from the ajax obj
//check that the logged element exists
if(typeof logged !== "undefined"){
//replace the content
$("#primary-content").html(logged.contents());
}
Sources
Detecting an undefined object property
Grabbing Content from Other Pages with Ajax and jQuery
load a specific element from an ajax loaded page
parse html string with jquery
Do you want to check if ajaresult contains "logged " ?
var primaryContent = '';
$.ajax({
url: 'ajax/test.html',
success: function(data) {
var loggedDiv = null;
loggedDiv = $(data).find('div#logged');
if(loggedDiv != null && loggedDiv != undefined){
primaryContent = $(data).find('div#primary-content').html();
}
}
});