User Friendly Url Ajax post - php

Problem solved
I'm using friendly url and code which is described below. I want to Request php file with ajax post. Does anybody know how to do that?
For example: I have code below:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "ajax/test.php",
data: "data=test",
success:function(data){
alert(data);
},
});
});
When program post data to test.php file then i want to also return data from this file.
Url: http://mysite.com/admin/admin.php?m=users&a=edit&id=12
User Friendly Url: http://mysite.com/admin/users/edit/12
Problem solved

I am confused what you are asking about. I will assume that you just want to return some data from test.php.
if so then assuming your $data variable holds all the data, you can do this
echo json_encode($data);
and that should alret you back the data as you have it in your code

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory_path/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</ifModule>
That's .htaccess file for user friendly url.
Thanks for help!

Related

Laravel delete method not allowed

I am writing a laravel api and when I try to make delete and post requests I keep getting a method not allowed exception. Where should I be looking to diagnose this problem?
I have read though most of the other posts on this issue and here is what I have tried/looked at.
• Made sure the route syntax was correct
• Made sure it didn't conflict with another route
• Made sure I was using the correct route (ran php artisan route:list to
double check)
• Modified the .htaccess folder (maybe I did this incorrectly) to allow GET, POST, PUT, DELETE
Here is what the route looks like in api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::delete('delete/{id}', 'LoginController#delete');
Route::get('stuff', 'LoginController#index');
Route::get('stuff1/{Username}', 'LoginController#show');
here is the function in the controller
public function delete(Request $request, $id) {
$user = Login::find($id);
$user->delete();
return "204";
}
here is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
</IfModule>
I can get around this issue by changing Route::delete() to Route::get() and achieve the same functionality but this doesn't seem like standard practice.
You have to set ajax type POST but send a parameter named _method with value delete like this:
$.ajax({
type: "POST",
data:{
_method:"DELETE"
},
url: productRoute,
headers: { 'X-CSRF-TOKEN' : productToken }
});
Or because you use laravel html form helper so it generates _method hidden input automatically so you'd better send all your form inputs such as token and method like this:
function()
{
var formData=$('#yourForm').serialize();
$.ajax({
type: "POST",
url: productRoute,
data:formData
})
});

Rewrite URLs in RESTful API

I have to create an API in PHP, which I have never done before. I have a index.php in my root folder which contains a form. A button click activates an AJAX Request to read all entries and a form can be filled out to send data. I will also have DELETE and update.
ajaxcall.js
// GET to retrieve
var req;
req=new XMLHttpRequest();
req.open("GET", 'src/api/v1/posts',true);
req.send();
//post with ajax
$.ajax({
type:"POST",
url: "src/api/v1/posts",
data: test,
ContentType:"application/json",
success:function(){
alert('successfully posted');
},
error:function(){
alert('Could not be posted');
}
});
I have a folder src/api/v1 in there I want the endpoint file api.php which handles the different requests. I now need all the AJAX calls to be resend to api.php which sits inside that v1 folder. I created an .htaccess which is stored in also stored in src/api/v1. I added the following:
.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule src/api/v1/(.*)$ src/api/v1/api.php?request=$1 [QSA,NC,L]
Unfortunately I get a 404 not found, it just does not seem to re-write the URL to the api.php so it is looking for /posts which does not exists. What am I doing wrong?
Actually you should store the .htaccess on the root directory, otherwise Apache will not know that there's an .htaccess under /src/api/v1, so apache will look for the directory src/api/v1/posts and as is it's not found a 404 error will be returned.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule src/api/v1/(.*)$ /src/api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

how to handle links path in a dynamic site using mod_rewrite for clean url

say i have a function:
function ajax()
{ $.ajax({
type:'POST',
url:'index.php',
data:{t:t},
success:function(data)
{ if(data)
{
alert(data);
}
}
i am using this function all over my pages,the problem is when i am in homepage
i.e :www/mysite.com it is fine but when i am in a page like this
www/mysite.com/modrewrite where modrewrite is not a folder but instead a mod_rewrite thing the ajax function returns 404 error,but when i modify it to ../index.php then it does not work in homepage.what should be done to make it work in all the pages!
Try this and see how it works for your ajax or use the full path.
function ajax(){
$.ajax({
type:'POST',
url:'/index.php',
data:{t:t},
success:function(data){
if(data){
alert(data);
}
}
});
}
Then in your .htaccess use this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-z0-9.]+)$ page.php?uri=$1

Ajax not working with this .htaccess

Im developing a CMS with PHP and MVC. I have the follow htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So i have a dispatcher to go to diferents views depends of the permalink on the url.
My problem is that im trying to use ajax for a Login and few things more and cant make it works.
Is there any expception in .htaccess to make this work or i only can put more exceptions on dispatcher to load the .php in ajax?
Anyone can help me?
Thanks in advance.
EDIT:
$.post
(
"../ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
I tried with:
"../ajax/login.php"
PATH+VIEW+THEME+"/ajax/login.php"
My folder structure is:
view
themes
standar
ajax
login.php
js
ajaxInteractions.js
index.phtml
As stated in the comments, you need to check your path or just use your absolute path like this:
$.post
(
"/view/themes/standar/ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
And one more thing; it is not always best practice to spread out the code like this ( it's good for PHP and plain JS calls ). For these types of calls, try to keep it more concentrated:
$.post( "/view/themes/standar/ajax/login.php", {
u:Base64.encode(user),
p:Base64.encode(password),
}, function(data) {
/** do your stuff **/
}, "json" );

htaccess rewrite and submit form not working together

after several researches a give up and post here , if someone could clearify me a things , it would be nice, thanks
When i use http://site.com/file.php?url=2012/08/15/My-html-page all works fine , ajax sends a post to file.php and display it , but when i use a rewrited by htaccess url http://site.com/2012/08/15/My-html-page.html post form do nothing , it dosent work
Links:
Original : http://site.com/file.php?url=2012/08/15/My-html-page
Rewrited with htaccess: http://site.com/2012/08/15/My-html-page.html
The database entry : 2012/08/14/My-first-post
To add a html agter pages i added
$url=$url.'.html';
$url= $_GET["url"];
My ajax that in file.php
<SCRIPT language="JavaScript">
$(function() {
$(".comment_button").click(function()
{
var element = $(this);
var note = $("#note").val();
var dataString = 'note='+note;
if(note=='')
{
$('#flash').fadeIn("fast");
}
else
{
$.ajax({
type: "POST",
url: "file_post.php",
data: dataString,
cache: false,
success: function(html){
$('body').load('/<? echo $url ?>.html');
}
});
}
return false;
});
});
</script>
my form that in file.php
<form name="comment" method="post" action>
<button class="comment_button" type="submit"><span>Отправить</span></button>
<textarea name="note" id="note"></textarea>
</form>
My php file in file_post.php
if(isSet($_POST['note'])) {
$note=$_POST['note'];
$note = html_entity_decode($note);
$date=date("Y-m-d");
$time=date("H:i:s");
$table = "insert into comment (note, date, time) values ('$note', '$date', '$time');";
mysql_query($table);
}
My htacces
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /
<Files inc/config.php>
deny from all
</Files>
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?url=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ file.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
the $_SERVER['REQUEST_URI'] on rewrited page gives me a empty input , its normal ?
Try changing your 2 rules to:
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?url=$1.html [QSA,L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ file.php?url=$1.html [QSA,L]
The .html is getting stripped so your file.php script is getting called like:
http://site.com/file.php?url=2012/08/15/My-html-page
instead of
http://site.com/file.php?url=2012/08/15/My-html-page.html
i dont know what was the error , something with a slashes or numbers and htaccess
but i found a solution with
RewriteRule ^read/([A-Za-z0-9\-_]+]*)$ file.php?url=$1 [L]
without html or php

Categories