hi anybody out there, i need help, how do i change the value of a URL argument?
Example:
URL: http://localhost/main.php?name='abc'&age='24'

how do i change the value of argument "name" and resend the information back to main.php without changing the age value.

Dani AI

Generated

A modern, simple way to change one query parameter while preserving the others is: read the request parameters into an array, change the single key you need, rebuild the query string with http_build_query(), then output a safe link or redirect. This preserves every other parameter and uses PHP’s built-in encoding rules. (php.net)

Important compatibility and security notes: do not rely on old patterns shown earlier in the thread (bare variables / registerglobals) — that feature was removed from PHP and is no longer available; and avoid POSIX ereg* functions, they were deprecated/removed in modern PHP. Use `preg*for regex work, access inputs through superglobals like$_GET, and always escape HTML output withhtmlspecialchars()and validate/sanitize input (for example withfilter_input()orfilter_var()`). (php.net)

Example (safe, minimal pattern — change only name, keep age and others):

<?php
$params = $_GET;               // explicit, modern access
$params['name'] = 'newName';   // modify single argument
$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
$href = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '?' . $query;
echo '<a href="' . $href . '">Send back to main.php</a>';

// or redirect:
// header('Location: ' . $_SERVER['PHP_SELF'] . '?' . $query); exit;
?>

Two quick troubleshooting tips: if the original URL is a full external URL (not the current script) use parse_url() + parse_str() to extract/modify its query before rebuilding; and always choose the appropriate encoding mode (PHP_QUERY_RFC3986 avoids + for spaces). (php.net)

Thanks to for the example approach — the workflow is the right idea — and to for experimenting; the above updates that pattern to current PHP best practices and adds the safety checks you’ll need.

Recommended Answers

All 2 Replies

Hi chunguy,

Try this code:


I hope that help to resolve,

fpepito

##### main.php

<?

if (isset($name)) {
  echo "You have enter with the name: <B>$name</B><BR><BR>\n";
}

if (isset($name) && ! isset($ok)) {
   $name = "new_name";
}

if (isset($name)) {
 echo "welcome <B>$name</B><BR>\n";
 echo "<A HREF=$PHP_SELF?name=$name&age=$age&ok=1>Reload this page</A><BR>\n";
} else {
 echo "<FORM ACTION=$PHP_SELF>\n";
 echo "Name : <INPUT TYPE=TEXT NAME=name><BR>\n";
 echo "Age : <INPUT TYPE=TEXT NAME=age><BR>\n";
 echo "<INPUT TYPE=submit value=\"Identify\">\n";
 echo "</FORM>\n";
}

?>

thanks. alternatively, i've found that you can actually the function ereg_replace().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.