Passing parameter via href php

Asked

Viewed 6,190 times

0

I’m passing a parameter via href, but wanted to hide the parameter in the URL.

I’m sent like this:

echo '<a href="edita.php?id=' . $objCont->getid() . '">';

I get it like this in the URL:

http://.../.../edita.php?id=38779

I wanted it to stay that way:

http://.../.../edita.php
  • I’m not from php area, but take a look at these 2 links: Stack Overflow Stack Overflow Note here that the suggestion is similar, make a form (Dynamic or not) and use input’s Hidden with the parameter in the input Value and use the post method.

  • 2

    post

2 answers

4


There’s no way, but by making a beautiful which in my view is unnecessary, the use of querystrings (?....) is just this, carry variables via URL for links use (Urls), if you are looking to make the URL look more beautiful really think you are worrying about something totally superfluous.

The way you can prevent the URL from changing is not to use the URL, switch with Ajax, using maybe a modal, for example with Bootstrap and <iframe > (or ) and the other would be using POST or even URL friendly

An example using Bootstrap with the attribute target="":

.modal-body > .editpage {
    border: none;
    width: 100%;
    height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

<a href="edita.php?id=1" target="name" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  Editar produto 1
</a>

<a href="edita.php?id=2" target="name" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  Editar produto 2
</a>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <iframe src="about:blank" name="info" class="editpage"></iframe>
      </div>
    </div>
  </div>
</div>

Using POST

In html it would look like this:

<form method="POST" action="edita.php">
    <input type="hidden" name="id" value="1">
    <button>Editar 1</button>
</form>

<form method="POST" action="edita.php">
    <input type="hidden" name="id" value="2">
    <button>Editar 2</button>
</form>

In php I would trade for this:

<?php

if (empty($_POST['id'])) {
    die('Falta o id');
}

$id = $_POST['id'];

It does not affect at all the use of POST and GET, but really POST has a lot of behaviors that can make you wonder, I’m not saying it’s right or wrong, but using something just to beautify the Urls is a little unnecessary.

URL friendly

If you use apache and have the rewrite module enabled you can use friendly urls too, of course it does not improve at all, at most reading, after all it makes no sense to index google editing pages

create a .htaccess thus:

 RewriteEngine on

 RewriteRule ^edit/(\d+)$ edita.php?id=$1 [QSA,L]

On IIS in the web.config:

    <rewrite>
        <rules>
            <rule name="editar" stopProcessing="true">
                <match url="^edita/(\d+)" />
                <action type="Rewrite" url="edita.aspx?id={R:1}" />
            </rule>
        </rules>
    </rewrite>

Or Nginx:

location /edit {
    rewrite ^/edit/(\d+)$ /edita.php?id=$1 break;
}

So it will direct Urls in format http://site/edit/1, http://site/edit/2 and http://site/edit/1001 for http://site/edit/edita.php?id=1, http://site/edit/edita.php?id=1, etc..

  • Another alternative to embellish querystrings is to use friendly Urls

  • @Don’t want to put Hermenascimento in the answer also as I would with url amigavél ? To leave something like this http://site/edit/1

  • @Kevin. Ready edited F, added examples with Apache (.htaccess), Nginx and IIS

  • @Felipenascimento added examples with Apache, Nginx and IIS to url amigavel, but personally I think it is quite firula for an internal/closed system, anyway it is for understanding.

  • So if I want to do the opposite ? From http://site/edit/edita.php?id=1 for http://site/edit/1 has as ?

  • @Kevin. F as so the opposite? Urls are like this http://site/edit/1 always with mod-rewrite, the user will see this http://site/edit/1 only the inner core of the Apache that sees this http://site/edit/edita.php?id=1 ;)

  • A ta, I traveled. Thank you.

  • Something else, in mine <a> need to change something ? Why not working. Continues like this http://.../.../edita.php?id=38779.

  • @Kevin. Is your server Apache? Not working http://.../.../edit/1 ?

  • Yeah, it’s Apache, and it’s not going like that.

Show 6 more comments

-1

Instead of passing the parameter by a href, pass it by a Session:

<?php
session_start();
$_SESSION['user_id'] = $obCont-getId();
header("Location: edita.php");
  • Is that I need to pass the ID by clicking on a product for example. That on the page you receive this ID is edited product information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.