I have a cool project where I need to upload an image via php/my_sql. That I can handle, but the images need to be linking to a certain url out of 100. In php can I save a url as a variable, then allow a drop-down menu of the 100 choices which point to a variable with a url?
The best choice would be to use an array:
$urls = array("url","url2","url3");
After you add all the 100 URLs in there, you can recurse through the array and output options into the tag.
<?php
echo "<select>";
foreach($urls as $current_url){
echo "<option>" . $current_url . "</option>";
}
echo "</select>";
?>
That would go through the array, echoing all the URLs into the tag.
If you don't want to set the text in the dropdown to the actual URL, you could set the array using keys array("This URL" => "url") etc. and put the URL value into the "value" property of the tag, and using the key name as the value between the opening and closing tags of the list.
If you need an explanation of that as well, I can provide one.
I'm still not sure what you mean, but if you want to know how to store a url in a variable that is usually done in a string like this:
$url = "http://www.mysite.com/the/beautiful/image.gif";
You can also redirect to that url like this:
header('Location: '.$url);
die();
If you want the user to decide to which site to go, do it similar to what BraedenP posted:
<select id="urls" onchange="document.location.href=document.getElementById('urls').options[document.getElementById('urls').selectedIndex].value;">
<?php
$urls = array(
'Image One' => 'http://www.mysite.com/one.gif',
'Image Two' => 'http://www.mysite.com/two.gif',
'Image Thee' => 'http://www.mysite.com/three.gif'
);
foreach($urls as $name=>$url){
echo "<option value=\"{$url}\">{$name}</option>";
}
?>
</select>
Related
I did many HTML dropdown (which generate select tags etc) with PHP scripts for different HTML files. Previously, I had as many dropdown scripts as there are HTML files. But now, I would like to have one script for all HTML files. For this, I need to have a variable 'id' attribut of the select tag which depend of the parameter of the url of each HTML page. I try to affect the parameter of the url to the 'id' attribut but without results.
Here is the url of one of the web page with a parameter 'name'.
http://127.0.0.1/projetwebtest/projetweb/indexSuperClass.php?name=superClass
And here is the code of the PHP script where I try to affect the url parameter value to the 'id' attribut.
One way :
$opt .= "<select class = 'custom-dropdown__select custom-dropdown__select--emerald' id = '<?php echo $_GET['name']; ?>' onchange = 'change();'>";
Another way :
$opt .= "<select class = 'custom-dropdown__select custom-dropdown__select--emerald' id = '$_GET['name'];' onchange = 'change();'>";
When I try to print with a 'echo' the url parameter $_GET['name'] it works but it doesn't in this case.
How can I affect the url paramter to the 'id' attribut ?
Thanks.
Your string is comprised of various uses of " and ' which is causing it to fail. Here's a working example. You can copy and paste it here to test it online.
$_GET['name'] = 'SELECT_TEST_ID';
$opt = '<select class="custom-dropdown__select custom-dropdown__select--emerald" id="' . $_GET['name'] . '" onchange="change();">';
echo $opt;
Sidenote: When you're creating HTML strings like these, and are not using a template engine, try using 'HTML String' instead of "HTML String", letting you avoid quote hell for HTML attributes.
I have a php form on my site. The form ultimately produces an article submitted by the user.
One of the data fields in the form, is a drop down menu so the user can select which publication they represent.
Currently, the form works if I am only trying to display the name of their publication.
However... I would also like that name to be a clickable link to their respective publication.
In an attempt to achieve this, I set my form up like this:
Form: <select name="publication" id="publication">
<option value="http://www.espn.com">ESPN</option>
<option value="http://www.cnn.com">CNN</option>
<option value="http://www.abcnews.com">ABC</option>
<option value="http://www.cbsnews.com">CBS</option>
<option value="http://www.foxnews.com">FOX</option>
</select>
And the echo is set up like this:
Echo:
<?php $publication = htmlspecialchars($_POST['publication']); echo $publication; ?>
Unfortunately, the result produces the full URL instead of the Text Link I am trying to achieve.
Not sure how I am supposed to code the form or the echo to achieve the desired clickable text link.
It is taking the value option, not the innerHTML. One solution is to make the value the following:
<a href='http://example.org'>Example</a>
Note: You may need to escape characters for this to work.
The value of $_POST['publication'] is going to be the chosen <option>'s value, not the display name.
If you want to display the link along with the name, consider encoding the list of publications into a static array, then outputting that:
$publications = [
"media1" => "link",
"media2" => "link"
]
if (array_key_exists($_POST["publication"], $publications)) {
echo '' . htmlspecialchars($_POST["publication"]) . '';
}
I want to write a simple php script to get URL visited from browser via PHP with:
$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]
It should look something like this:
<?php
...some stuff to print out the variable...
?>
Choose one or use some vars did you needed from $_SERVER vars:
foreach($_SERVER as $key => $value){
echo '$_SERVER[\''. $key .'\'] => '. $value .'<br /><br />';
}
above will displaying all value.
maybe you need create new db table with column ip and url_visited.
I'm working on a code like this:
<?php
$id=$_POST['id'];
$url_tag = $_POST['url_tag'];
$url_back = 'https://www.page.example.com/page.php?';
$query='id='.$id.'&url_tag='.$url_tag;
$url = $url_back.$query;
echo 'Look how this url shows up: '.$url;
echo '<a href='.$url.'>Click here</a>';
?>
This is, the page receives two POST parameters. Then prepare a link to https://www.page.example.com/page.php? and I append those two parameters as GET parameters with the ids id and url_tag respectively.
Then I display how the whole link looks like. It shows up correctly, in this case https://www.page.example.com/page.php?id=ID&url_tag=URL_TAG, where ID and URL_TAG are the actual values received as POST parameters.
However, when I click on the 'Click here' link, it redirects me to https://www.page.example.com/page.php?, which is the url without any GET parameter.
Why is that happening and how would I solve it? I've tried to feed HREF with urlencode($url) instead, but it redirects me to an address flooded with undesired characters...
Any idea? Thank you!
Try to replace the last line of your code by this:
echo 'Click here';
It should work.
Try using http_build_query(), it takes care of any URL character compatibility issues for you...
// assuming you've already checked and validated your $_POST parameters
$query = http_build_query(array(
'id' => $_POST['id'],
'url_tag' => $_POST['url_tag']
));
$url = 'https://www.page.example.com/page.php?' . $query;
?>
Click here
I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1