See the following code. I am trying to open ‘index.php’ with a variable named ‘section’ filled in with one of the values clicked on.
Is there a ‘cleaner’ way to send information with a php url in a link?? as a bonus is there a way to do it so that the information isn’t shown in the address bar (looks cleaner that way)
<?php
$connection = mysql_connect(xxxxxxxxxxxxxxxxxxxxxxx);
mysql_select_db(xxxxxxxxxx);
$query = "select distinct section from photos order by section";
$result = mysql_query($query) or die ('Error in query: $query.' .mysql_error());
while($row = mysql_fetch_row($result))
{
echo "<li><h3><a href=index.php?section=". $row[0].">". $row[0]."</a></h3></li>";
}
?>
You could make it “cleaner” looking (and more search-engine friendly) using URL rewriting. Basically, you’d add a few lines into your .htaccess file and then variables can be in the URL without really looking like variables are in the URL. So…
When I submit a php file using a form none of the variables get shown in the url.
I’m trying to achieve what a form does without using a form or buttons…
In the hope of explaining a bit better…
the above code constructs a list of sections which are also links.
when I click on one of them I open the index.php file which reads from _POST what link was clicked on, and then runs a query on a database selecting only those tables that have a ‘section’ of what was clicked on.
So if I click on ‘Bikes’ Index.php will only show my images that are in the ‘bikes’ section (the details of index.php are unimportant here… I am trying to send the information TO index.php)
EDIT: Ps. I’ve tried googling the answer to this but it’s almost impossible to know how to word the ‘question’ for google without getting wildly irrelivent results.
After the user clicks a link does it stay on the same page? If you want to retreive info and insert it into a <div> or <span> or whatever, you use AJAX.
If you want to reload the page, I don’t think their is anyway to POST an href or anyway to use GET without it displaying in the browser’s Location bar. Now you could do some javascript tricks where they click the link, it put the values in a <input type=“hidden” and then you submit a form.
I don’t think there’s a way to do what you’re talking about exactly, but Google “URL rewrite” to get a basic recreation of it… basically what I already said, but it goes like this:
Forms pass on a ton of information via the POST and GET commands… a stand text link, not so much. Your other alternative would be to make a form button and style it to look like a text link (elminate background, border, change the hover to the hand icon, etc.), but then you’re into another mess of problems.
I appreciate the help wasson but the url hiding is secondary… My main aim is to send the data regardless of how it looks in the url adress bar… then once I’ve got that sussed I can concentrate on hiding the data in the url.
When I get to that point I’ll research Url rewrite (I did google it… it looks like a lot to take in)
So far I have built the page using the html forms ‘select’ feature. See code below
**
<center>
<h1>My Images</h1>
<?php
include ‘…/includes/db.php’;
if ($_POST[‘section’] <> “”)filter = "where section = '"._POST[‘section’].“'”;else $filter = “”;
$connection = mysql_connect(‘localhost’,$sqlusername,$sqlpassword);
mysql_select_db($sqldbname);
$query = “select * from photos “.$filter.” order by section”;
$images = mysql_query($query) or die (‘Error in query: $query.’ .mysql_error());
$query = “select distinct section from photos order by section”;
$sections = mysql_query($query) or die (‘Error in query: $query.’ .mysql_error());
PHP is looking for a variable in POST format - coming from a form.
Like in ASP if I said:
Request.Form(“myvar”)
It would not get info from the querystring. I’d need to use this:
Request.Querystring(“myvar”)
Or, I could use:
Request(“myvar”)
which would grab the value from a post or a qstring but obviously this is a bit of a security risk (people can just start hacking your query strings).
Anyway, looks like your PHP is requesting data in the PHP equivalent of “Request.Form” so that’s why it’s not responding at all to stuff in the querystring.
I am not a PHP pro so that’s all I can tell you. Maybe change the _POST in the code to _GET and see what that gets you.
There are a couple good tutorials on this online: This one is for if you have multiple pages, andthis one is just a general script (which looks like the one you posted).
Your “index.php” file needs to read the variable in the URL and act accordingly… so, if the URL is ‘index.php?section=misc’, then one of the first lines in ‘index.php’ needs to be:
$var = $_GET[section];
if ($var == 'misc') {
echo "display miscellaneous information here!";
}
That is what I’m asking. Though I’ve been using $_POST. As you and ZipperJJ have said it will work if I use _GET , however this method leaves the information visible in the address bar.
In this case that isn’t too much of a problem, but now I can start researching the url rewrite thing!
(I say ‘now’ I mean tomorrow… now I’m going to bed)
See http://www.peterlobley.co.uk/photos/main.php for the end result of today’s work.
index.php uses both get and post. get if being called by main.php. post if being called by itself from the dropdown menu.
p.s. thanks to all for the help so far. Sorry if I tend to be an annoying pupil.
I Just want to bump this to let anyone who’s interested know that I found a solution in the end that doesn’t use $_GET (the php equiv of asp’s querystring)… after settling for the workaround and working on other things… last night the solution walked into my brain while I was in bed.
I don’t know how to do it with sessions. I’m sure it’s simple but I wanted to see if it was possible to do it the way I originally thought.
Anyway… last night I thought “why not have a whole form for each link? and change the values of hidden inputs for each seperate link”
I think this was hinted at by some of you in your replies so if you see any of you in the following then the credit is yours… The php
<?
include '../includes/db.php';
$connection = mysql_connect('localhost',$sqlusername,$sqlpassword);
mysql_select_db($sqldatabasename);
$query = "select distinct section from photos order by section";
$result = mysql_query($query) or die ('Error in query: $query.' .mysql_error());
$count=0;
while($row = mysql_fetch_row($result))
{
echo "<form name=links".$count." method=post action=index.php>
<h1><a href='javascript:document.links".$count.".submit()'>". $row[0]. "</a></h1>
";
echo "<input type=hidden name=section value=".$row[0].">
<input type=hidden method=submit>
</form>
";
$count++;
}
?>
And it works a treat. I know that apparently using links to submit forms is naughty/bad but the alternatives I’ve seen on google seem worse - disguising a button as a link. and the main thing is I achieved what I set out to achieve.
It’s a pretty interesting trick, but I have to confess that I don’t quite understand why you’re concerned with the URL looking “cleaner.” In fact if you keep the query string parameter, the user can bookmark the page once he gets there, but with a POST, he can’t. Also with a POST, if he back-buttons his way back to the result page, he will get that browser popup saying that he has to re-post the information, but he has no idea he ever posted anything; he only clicked what appeared to be a link. And how about search engines–will they be able to automatically follow the “links” and index the different sections?
I’m not really a UI kind of guy but I honestly feel that POSTs are best suited for submitting information rather than accessing it.
One last thing, if you really need to use POST, couldn’t you use one form and have the onClick() of the link change the value of the hidden input in the form, and then submit()? That way you don’t need to make a new form for each link … maybe I’m missing something?
You’re not missing anything arseNal. I could do that. In my experience though I might have ended up pulling my hair out to get that to actually work.
By having a form for each link the most complex bit of java I have to do is run the ‘submit’ method for whichever link (form) is clicked on.
And to adress the other bit of your post… By tradition I am an application programmer (if I’m any sort of programmer at all) and to my mind exposing the vars in the url would be like exposing my C++ code in an application.
There’s nothing wrong with it. I guess now that I know how to do it - I can choose when to do it and when to expose the variables.
Lobsang, I’d advise against that method and just put the variables in the URL. It’s easier, cleaner, faster loading, and more.
Google will also crawl to pages that are linked, but won’t follow those forms. There’s also the aforementioned problems with bookmarking, navigation, etc. There’s also problems with accessibility.
If you want clean URLs, you want to pass the variable in the URL and use URL rewriting to make them look prettier. Google will love you, as will the Web Standards Gods.
I realize these issues may not effect your certain situation (you don’t care about SEO or accessibility, maybe), but it’s always a good practice to do it the “right” way.