AlwaysLearning AlwaysLearning - 9 days ago 6
PHP Question

Remove parameters from URL and paste finished string to address bar

I am new to php and I would appreciate any help you could offer. I wish to remove the parameters from a URL. I can remove them using the code below but my question is how can i parse the result into the address bar. I don't want to reload the page via

header("Location: index.php")
because i lose the use of the parsed variable. I have looked at other questions but they do not tell you how to parse to finished string into the address bar. Here is my code below.

<?php
if($_REQUEST["add"])
{
$message = "Item has been added";
$url = $_SERVER['REQUEST_URI'];
$url = strtok($url, '?');
}
?>
<head>
</head>
<body>
<?php
echo $message;
?>
<a href="?add=item">Click Add</a>



Answer

If I read your question right, you want to load only the page url without the parameters while still doing the operation you requested.

There are 2 ways to do that.

-- Use a form with method post.

<form method='post'>
    <input type='hidden' name='item' value='item' />
    <input type='submit' name='add' value='Add Item' />
</form>

Then, in PHP, you can get the value of the added item from the POST variable like this

if(isset($_POST['submit'])) {
    //do something with $_POST['item']
}

-- Send the value to a session variable and reload the page

if(!isset($_SESSION['added']) && isset($_GET['add'])) {
    $_SESSION['added'] = $_GET['add'];
    header('location: '.strtok($_SERVER['REQUEST_URI'], '?'));
}