I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.
Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.
So, the querystring looks like this:
http://www.foo.com/script.php?bar1=green&bar2=blue
But when a link or 'remove' button is clicked by a user, bar1=green is removed, and the visitor is directed to the following URL:
http://www.foo.com/script.php?bar2=blue
I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.
Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpage by a PHP wrapper using an iframe.
Any suggestions greatly appreciated.
Many thanks,
Matt
You can remove the value from the query string using this code:
<?php
function parseQueryString($url,$remove) {
$infos=parse_url($url);
$str=$infos["query"];
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
if(isset($op[$remove])){
unset($op[$remove]);
}
return str_replace($str,http_build_query($op),$url);
}
echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
?>
Related
i am really new to stackoverflow and actually dont have any clue about php.
I have a json array which looks like
[{"betreff":"h"},{"betreff":"VS Code fehlt"}]
i´ve created (copied :D) a foreach loop
to output the "h" and "VS Code fehlt"
function display_array_recursive($json_rec){
if($json_rec){
foreach($json_rec as $key=> $value){
if(is_array($value)){
display_array_recursive($value);
}else{
echo '<a href="#" onClick="test("'.$value.'")">';
}
}
}
As we can see now the "h" and "VS Code fehlt" parts are being outputted as links. I want to make it so that whenever I click on the link a new get requests should be sent out with the giving value.
function test($value){when pressed = send new file_get_contents request to localhost/api/ticket/{$value}}
i hope i could describe good enough what i want.
Thanks in advance, please inform me if anything is unclear
You can't use PHP to make GET requests like that (unless you use a hidden IFRAME, as far as I know). You need to use JavaScript for that, using any of those APIs (if you're using vanilla JavaScript):
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Example:
<script>
function test(url)
{
fetch(url);
}
</script>
<?php
function display_array_recursive($json_rec)
{
if (!$json_rec) {
return;
}
foreach($json_rec as $key=> $value){
if (is_array($value)) {
display_array_recursive($value);
continue;
}
echo '<a href="#" onClick="test(\''
. htmlspecialchars(
$value,
ENT_QUOTES|ENT_HTML5,
'UTF-8'
)
. '\')">Text</a>';
}
}
Notice the function display_array_recursive is escaping the HTML. It's also using ' (single quote) for the test arguments. It's already using " (double quote) for the element attribute values.
I suggest you to read:
How do I run PHP code when a user clicks on a link?
https://www.delftstack.com/howto/php/onclick-php/
https://johnmorrisonline.com/prevent-xss-attacks-escape-strings-in-php/
https://stackoverflow.com/a/3996696/4067232
https://betterprogramming.pub/refactoring-guard-clauses-2ceeaa1a9da
my code is related to breadcrumbs.. that is it should display previous page or from where it is navigated and i achieved it partially , while im refreshing 2-3 times im getting the current page not the previous page.. so pl help me on this
my code lies in session.php as
$add = $_SERVER['PHP_SELF'];
if($_SESSION['pageadd'][1]!= $_SESSION['pageadd'][2])
{ $_SESSION['pageadd'][2]= $_SESSION['pageadd'][1];
}
echo $_SESSION['pageadd'][2];
if(($_SESSION['pageadd'][1]!= $add) )
{ $_SESSION['pageadd'][1]= $_SESSION['pageadd'][0];
$_SESSION['pageadd'][0]=$add;
}
What you want isn't a breadcrumb - it's a history for visited pages! This could be achieved with something like this:
if (!isset($_SESSION['pageadd'])) {
$_SESSION['pageadd'] = array();
}
// add page
$_SESSION['pageadd'][] = $_SERVER['PHP_SELF']
// only save last 5 pages
if (count($_SESSION['pageadd'])) > 5) {
array_shift($_SESSION['pageadd']);
}
Try to use $_SERVER['HTTP_REFERER'] it will return you previous url. However you need to store it in some hidden field or session like you are doing now.
Hope this help :)
I am working on PHP code for automatically redirecting a URL to an affiliate URL based on words in the URL.
This is the code I am using:
if (stripos($mylink, "amazon.in") > 0) {
if (strpos($mylink, "?") > 0) {
$tmp = $mylink."&affiliate_id";
} else {
$tmp = $mylink."?affiliate_id";
}
$loc = "Location:".$tmp;
header($loc);
exit();
}
But the problem is that when the Amazon URL is like: http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?ie=UTF8&node=4068584031 it adds the affiliate id like this: ie=UTF8&affiliate_id (it automatically removes the node and its value) but I want it to be like: ie=UTF8&node=4068584031&affiliate_id.
This means that I need to add &affiliate_id at the end of the URL every time if it is an Amazon link.
Actually I just want to achieve the following simple thing: if the URL doesn't have any ?s, I need to place ?affiliateid and if the URL has a ?, I just need to append &affiliateid at the end.
Can you suggest the very simple method for implementing what I require?
Let PHP do the string parsing for you. Let's say your affiliate id is XYWZ.
if (strpos($mylink, "amazon.in") !== 0) {
$urlarray=parse_url($mylink);
$urlarray['query']='affiliate_id=XYWZ&'.$urlarray['query'];
$newurl= $urlarray['scheme'].'://'.$urlarray['host'].$urlarray['path'].'?'.$urlarray['query'];
echo $newurl;
}
will output
http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?affiliate_id=XYWZ&ie=UTF8&node=4068584031
see? I didn't need to worry if there's a question mark. It will be stripped in the output of parse_url;
Edit: using $mylink instead of $theurl
let say I have an url like this
http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj
Now i want to update p2=100 and reload the page using php
here parameters can be unlimited (p1,p2,...pn), and we can update any param and reload the page.
Fairly simply, you can do this
$_GET['p2'] = 100;
header("Location: http://www.domain.com" . $_SERVER['REDIRECT_URI'] . '?' . http_build_query($_GET));
The question is kind of vague, but assuming you want to reload from the client side using javascript:
window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
Reload your page you just have to setup your variables the way you want it in the URL field
If you want to reload page with desired parameters use JS
Following script might help you
window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
window.location = "http://www.domain.com/myscript.php?p2=200&p1=dfgb&p3=asdhahskh&etc=alotofparameters"
Now if you want to reload the page after a specific amount of time interval then you can use the following meta tag
<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">
Njoy Coding.
:)
Here is what I use when I want to change 1 $var value and then redirect.
function getUrlWithout($getNames){
$url = $_SERVER['REQUEST_URI'];
$questionMarkExp = explode("?", $url);
$urlArray = explode("&", $questionMarkExp[1]);
$retUrl=$questionMarkExp[0];
$retGet="";
$found=array();
foreach($getNames as $id => $name){
foreach ($urlArray as $key=>$value){
if(isset($_GET[$name]) && $value==$name."=".$_GET[$name])
unset($urlArray[$key]);
}
}
$urlArray = array_values($urlArray);
foreach ($urlArray as $key => $value){
if($key<sizeof($urlArray) && $retGet!=="")
$retGet.="&";
$retGet.=$value;
}
return $retUrl."?".$retGet;
}
This takes the url ($_SERVER['REQUEST_URI']), removes the desired values ($getNames) [which can be one or more values], and rebuilds the url. It can be used like-
$newurl = getUrlWithout(array("p2"));
header( 'Location: http://www.domain.com/'.$newurl.'&p2=100' );
Try below codes:
$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = preg_replace('/p2=([0-9]*)&/', 'p2=100&', $varURL);
header('location:'.$varNEwURL);
OR
$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = $varURL.'&p2=100';
header('location:'.$varNEwURL);
I have a php web page that has 2 separate forms on it that both show results at the bottom of the page. The first form uses one drop-down menu (titled Quickshow) with 6 choices that filters the results when selected, with the top choice being the default selection when the page is opened. The second form has 6 drop-down menus, each with multiple choices, that filters the results once the "Filter Results" button is clicked.
My problem is when the second form is used, it is using the default selection from the first form instead of staying with the selected choice from the first form. I understand the code that is making the first choice the default, and also allowing it to change for the first form, but how do I keep (call?) the optional choices for the second form? Below is the code being used for both forms. The first part is the web (.php) page portion, the second part is on the template (.tpl) page that is being pulled over to the web page. I didn't write the pages, but am trying to fix the filter on it.
.php page
function enumRequests() {
$getQuickShow = 1;
if (!$_REQUEST['feature_quickshow'] == '') {
$getQuickShow = (int)$_REQUEST['feature_quickshow'];
}
$quickShow = eval(quickShow($getQuickShow));
$whereArray[] = (string)$quickShow;
if ($_REQUEST['resultsFiltered']) {
$quickShow = eval(quickShow($getQuickShow));
//$whereArray[] = (string)$quickShow;
foreach ($_REQUEST AS $key => $val) {
if ($val) {
$val = mysql_real_escape_string($val);
if (strpos($key, 'fld_') === 0) {
$newKey = str_replace('fld_','',$key);
$whereFragment = "{$newKey} = '{$val}'";
$whereArray[] = (string)$whereFragment;
}
}
}
}
}
.tpl page
Quick Show:[#quickshow]
Thanks in advance for any help I receive with this.
you can always refer to document.forms[1].mytxt.value = document.forms[0].utxt.value