Passing parameter via href php

0

I'm passing a parameter via href , however I wanted it to hide the parameter in the URL.

I'm like this:

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

I get this in the URL:

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

I wanted it to look like this:

http://.../.../edita.php
    
asked by anonymous 22.05.2017 / 18:43

2 answers

5

There's no way to make a beautiful a gambiarra that in my point of view is unnecessary, ( ?.... ) is just this, transporting variables via URL to use links (URLs), if you are looking to make the URL look more beautiful, I really think you are worrying about something completely superfluous.

The way you can do to prevent URL from changing is not to use URLs, switch with Ajax, maybe using a modal, for example with Bootstrap and <iframe > (or

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

.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><scriptsrc="//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 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 would trade for this:

<?php

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

$id = $_POST['id'];

It does not affect the use of POST and GET in any way, but POST really has a lot of behaviors that might make it strange, I'm not saying it's right or wrong, but using something just to beautify the URLs is a little unnecessary. / p>

Friendly URL

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 does not make sense to index google edit pages

create a .htaccess like this:

 RewriteEngine on

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

In IIS in 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 the format http://site/edit/1 , http://site/edit/2 and http://site/edit/1001 to http://site/edit/edita.php?id=1 , http://site/edit/edita.php?id=1 , etc.

    
22.05.2017 / 20:04
-1

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

<?php
session_start();
$_SESSION['user_id'] = $obCont-getId();
header("Location: edita.php");
    
22.05.2017 / 18:52