I am developing a website for a client using html, js and php. For any database call i am using ajax call
$.ajax({
type: 'POST',
url: "index2.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
dosomething(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
}
});
and php pages returns me the output. The problem is that index.html page url is not getting changed.....I want it to change say index.html?id=23 (creating query string in html page)
Basically my aim to make it SEO friendly url like index.html/helloppl
What is the best method to do this. While googling i found link for doing....still i need to create query string here...any help is appreciated.
Creating SEO fridenly url.
Creating query string in html
The problem is that index.html page url is not getting changed. I want it to change say index.html?id=23 (creating query string in html page)
Ans: if you want to change the url like you have mention above this the ajax will not the way to do this . This can be done in simple way go to the url i have provided and look at the topic.
http://html.net/tutorials/php/lesson10.php
Basically my aim to make it SEO friendly url like index.html/helloppl. What is the best method to do this. While googling i found link for doing this. I still need to create query string here.
Ans: This can be done with htaccess. Go To The Link
Jquery ajax() messing with my .htaccess mod_rewrite
Related
I'm trying my first PHP MVC project from scratch. Everything is working fine... everything but ajax calls. When I try to POST I get a 404 error, but ONLY using AJAX; I've made the same calls using html form and it works.
This is my request:
$.ajax({
type: "get",
url: "/Prueba/Bodega/createPost",
data: data,
success: function (data) {
and this is on my view (works if I comment the "preventDefault" line on js)
<form action="<?=BASE_URL?>Bodega/createPost" method="POST">
Both methods calls the controller Bodega, function createPost ("http://localhost/prueba/Bodega/createPost").
I think this is because of the .htaccess file using friendly urls but I coundn't get the solution.
This caused because the we server treats the request URLs in a case sensitive way. That said:
/Prueba/Bodega/createPost and /prueba/Bodega/createPost are two different URLs.
Please change /Prueba/Bodega/createPost to /prueba/Bodega/createPost and it should get fixed.
Please use POST on ajax request.
hello there ive created a site that have the pagination using ajax request to change its content using ajax that request sql data and print it to html codes.
the first site : pilem21.com
this one work just fine,
but the second site wont work as i hoped.
the second site : nontonoke.com
is the anything wrong with the server? or is it on my code?
here is the example of my codes requesting the pagination data
var a = $(".film2numb").val();
return $.ajax({
type : "GET",
url : "php/controller1.php?page=semuafilm",
data : "data="+a,
success: function(data){
$('.semuafilm').load('php/film.php',function(){$("img.lazy").lazyload();});
},
});
I have a web application, the front end is HTML & JavaScript/jQuery and The back is PHP.
It was using ugly URL's like so, host/app/Page.php?userid=1.... Ugly!
So with a little MOD_REWRITE it will now serve the urls as url/page/1/
RewriteRule ^page/([0-9]+)/?$ host/app/page.php?userid=$1 [NC,L]
Next it originally did simple jQuery GET request to the PHP files to get the dynamic data.
var params = A JS function to get all paramters after the ? in URL
var userid = params.id;
$.getJSON('host/app/page_code.php',{'userid':userid}, function(data) {
var blah = data.blah;
});
Now after using MOD_REWRITE I now have no nice way of getting the front end to the parameters, only in a page/1 format. Looping though and getting each parameter between the slashes just doesn't feel like the right way to do it.
So I hit Google and it seems that sending the parameters from MOD_REWRITE seems like the way to do it. So my questions are, how is this possible, how is it done?
How do I get the parameters from the URL with MOD_REWRITE, send those to the PHP back-end, then return a response to the front end with JSON?
edit: The problem I have is I need to pass Parameters from JS to a PHP back end then get a response. I originally got these parameters from the URL with JS, then passed them into the GET request, I would then get a response from the PHP depending on the Parameter sent. However now I have used mod_rewrite on the URL I can not see the parameters on the front end to catch in the traditional ?a=b&c=d format. it now looks like /b/d To get these in JS I can only do this by looping through and getting each parameter between the slashes. Doesn't seem right to do it like this. I need to actually some how get the parameters to the PHP code in a separate page from the front end. Then pass it back to the front end with the response from the PHP code so I have lets say the users id. I don't want any php in the front end file as to keep it all separated. There must be a way to get the parameters from the url to the PHP code in a different file, then send back a response to the front end HTML&JS with the usersid. I'm new to this whole MOD_REWRITE for clean urls, and it's confusing the hell out of me!
You could write a reverse router with javascript in which given a URL, it returns the parameters.
Example (really bad one, just to give you an idea) on jsfiddle
Drawbacks: you will have to copy the routings from .htacces file to Javascript. Everytime something changes in that file, you will have to change in Javascript too.
Edit:
Another approaches
Do all the routing processing in PHP, not even using .htaccess and then retrieve data for routing in JSON format. You'd access this from an Ajax request
Example:
jQuery.ajax({
url: 'reverse_routing.php',
type: 'json',
data: {
url: window.location.pathname
},
function(params) {
// Do your stuff here...
}
});
use a post method with http://api.jquery.com/serialize/
Example from jquery.
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $(this).serialize() );
});
An idea to pass the data in a PHP sense.
data = $( "form" ).serialize();
$.post("serverside.php", data, function(returnData)
{
$("divid").html(returndata)
}
PJAX's documentation states that Github uses $.pjax.submit() in submitting a Gist form. A desired feature of an ajax form submission which Github nicely implements is that the URL redirects from the form's action to a newly created URL (in this case, one containing the newly server-side created gist ID).
For example, from this:
https://gist.github.com/gists // form action
to this:
https://gist.github.com/tim-peterson/5019589 //assume this ID is generated server side
I've gotten this to work similarly on my site (i.e., the page itself redirects to the equivalent of https://gist.github.com/tim-peterson/5019589) but I can't redirect the URL (it stays like https://gist.github.com/gists).
Is this entirely a server-side issue (setting headers?) or is there something in pjax that I'm missing? I'm using a version of pjax I downloaded today so it can't be that i'm using a buggier version of pjax.
Did you find any solution to this?
I had the similar issue.
Basically you need to set X-PJAX-URL property in response header.
It's value should be same as Request URL.
In Laravel, I did it like this...
App::after(function($request, $response)
{
$response->headers->set('X-PJAX-URL', $request->getRequestUri());
});
There may be a more-elegant / proper way to do this with $.pjax.submit(), but this is the solution I've been using without any problems. I serialize the form inputs prior to submission, then POST them to my server. Then check the reply from the server to make sure I don't need to prevent the user from going forward before calling my PJAX to continue.
$("#save").click(function(e) {
e.preventDefault();
dataString = $("#form").serialize();
$.ajax({
type: "POST",
url: $("#form").attr("action"),
data: dataString,
dataType: "json",
success: function(data)
{
// Handle the server response (display errors if necessary)
$.pjax({
timeout: 2000,
url: data.url,
container: '#main-content'
});
}
});
});
It happens if response HTML doesn't contain initial PJAX container
Currently I have two sites hosted on different domains.
SITE 1: I want to use as a CMS for content creation only.
(writing,editing,etc.)
SITE 2: I want to use for viewing content.
Theoretically, what I'd like to be able to do, is create multiple peices of content in SITE 1.
When I'm ready to publish these I'll press a 'publish' button, which will alert SITE 2 that there is new content and pass the values via JSONP.
SITE 2 will then pull in the data via JSON and do what it wants...(enter into DB, etc.)
...so yeah I'm stuck at the theoretical part. I'm not exactly sure where to go now but here's what I'm thinking.
There's a javascript function on SITE 1 that is called when I choose publish:
$.ajax({
type: "POST",
dataType: "jsonp",
data: postData,
url: 'http://site2.com/admin_json_controller.php',
success: function(data) {
// 'data' is a JSON object which we can access directly.
// Evaluate the data.success member and do something appropriate...
if (data.success == true){
alert('worked!');
}
else{
alert('did not work!');
}
}
});
This posts a json object to a php file on SITE 2. This file will simply be waiting for this post object. When it recognizes the post object...it will then enter the values into the database.
Does this make sense? I have a feeling I don't quite understand JSON yet but any help, questions, tips, pointers are much appreciated.
THANKS
To simplify things you have two options:
Use the same database for both domains.
Do a simple post to the other domain. Set the action of the form to script on the other domain. The other domain can access the variables via $_POST.