简体   繁体   中英

Allow $_POST variables with rewrite rule .htaccess

I'm triyin to do a page using MVC with PHP and Smarty, and semantic URLS (URL-rewriting).

So i have a main controllers that reads the name of controller and its action.

Example: /mypage/object/edit

the main controller after the rewrite reads: ?subtopic=object&action=edit

where "edit" is a function of the class "object".

So, i have a form in a .tpl (Smarty template) like this:

<form name="formulario" action="/mypage/imagen_del_dia/guardar" method="POST">
<table>
<tr>
<td><div>Autor</div></td>
<td><input type='text' id='autor' value="{$username}" readonly /></td>
</tr>
<tr>
<td><div><input type='hidden' id='id' value="{$imagen.id}"/></td>
<td><input class="button_personalizado" type='submit' value='Editar' name='Editado'></td>
</tr>
</table>
</form> 

When you clicked in submit, the .htaccess do this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule    ^([0-9a-z_-]+)$ /mypage/index.php?subtopic=$1 [NC,L]
RewriteRule    ^([0-9a-z_-]+)/guardar$ /mypage/index.php?subtopic=$1&action=save [NC,L]

The idea is that the $_POST data can be read by:

/mypage/index.php?subtopic=$1&action=save

In this case the main controller open the class "imagen_del_dia" and its function "save"

The problem is that the $_POST data is lost in that point.

@Zerkms Thank you so much, you are right!

It's right the problem was the form, because i was using that form for a AJAX function, so the input had "id" tags, and in HTML must be "name" tags.

So i rename all "id" for "name", and now works fine!

Example:

<td><div><input type='hidden' name='id' value="{$imagen.id}"/></td>

Another tip is that i wasn't putting the print_r($_POST); in the right place, because my page is a class, you need put in the end of the class before of the ?> tag in your PHP page.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM