FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Send POST variables from HTML





ninjakannon
I'm trying to build a system wherein I have one single 'index.php' page that displays my html web pages dependant on a POST variable sent to it.

But I have a problem. How, on my html pages, do I send a POST variable dependant on the hyperlink the user clicks on - preferably without using JavaScript. Using GET would be easy, I'd just do something like:

Code:
<a href="index.php?pageid=1">My Link</a>


How would I do the same with POST?
MrBlueSky
With Javascript I can help you with it. But without Javascript? I don't know if that's even possible..
ninjakannon
MrBlueSky wrote:
With Javascript I can help you with it. But without Javascript? I don't know if that's even possible..

I did wonder whether it was possible, but didn't find anything when searching Google.

I guess I'll have to use JavaScript then. So yes, please explain.

Thanks a lot, MrBlueSky! Very Happy
MrBlueSky
We just use a POST form, only we make it invisible to the user and fill it in and submit it by using javascript:

Code:

<SCRIPT language="JavaScript">

  function goto(pageid) {
     document.hiddenform.pageid.value = pageid;
     document.hiddenform.submit();
  }
 
</SCRIPT>

<FORM method="POST" action="process.php" name="hiddenform">
  <INPUT type="HIDDEN" name="pageid">
</FORM>

<a href="" onclick="goto(1); return false;">Page 1</a>
<a href="" onclick="goto(2); return false;">Page 2</a>
<a href="" onclick="goto(33); return false;">Page 33</a>


When the user clicks a link, the function goto fills in the form and submits it. That's all.
ninjakannon
Oh that's simple! Thanks a lot. Very Happy

One thing; I want my website to be usable by as many people as possible - of course including those who have JavaScript turned off.
I've never looked at the options concerning JavaScript in detail, but when I was using a computer with JavaScript turned off, some still worked. Would links using the method you have shown above still work if JavaScript was turned off? Or was that just an odd case?

Also, do you know whether a large number of people turn off JavaScript? Or would the large majority of people visiting my website still be able to use the hyperlinks?
kv
I don't think lot of people turn off javascripts. Probably only those who have really sensitive data on the systems (like bank account data, etc). However, if you still want to do it without using javascript, here is the way.
Code:

<FORM method="POST" action="process.php" name="form1">
  <INPUT type="HIDDEN" name="pageid" value="1">
  <INPUT type="submit" name="submit_button" value ="Page 1">
</FORM>

<FORM method="POST" action="process.php" name="form2">
  <INPUT type="HIDDEN" name="pageid" value="2">
  <INPUT type="submit" name="submit_button" value ="Page 2">
</FORM>

<FORM method="POST" action="process.php" name="form3">
  <INPUT type="HIDDEN" name="pageid" value="3">
  <INPUT type="submit" name="submit_button" value ="Page 3">
</FORM>



and so on. This displays a button instead of link, but you can use CSS to make the button look like a link.
JayBee
ninjakannon wrote:
Would links using the method you have shown above still work if JavaScript was turned off?


you can use
Code:
<a href="non-js-page.php?pageid=1" onclick="goto(1); return false;">Page 1</a>

when you click with mouse wheel or when you have javascript turned off, the next page will be non-js-page.php?pageid=1




If you cant make your script able to handle GET requests, you can also create a wrapper Very Happy
just use function described here http://php.net/manual/cs/function.fsockopen.php#49938
and create wrapper.php
Code:
<?php
echo post("yourserver.com/index.php","pageid=".$_GET["pageid"]);
?>

and call this script wrapper.php?pageid=1
of course, you will have problem with headers. This is only direction of another way Wink
mathiaus
kv wrote:
I don't think lot of people turn off javascripts. Probably only those who have really sensitive data on the systems (like bank account data, etc). However, if you still want to do it without using javascript, here is the way.
Code:

<FORM method="POST" action="process.php" name="form1">
  <INPUT type="HIDDEN" name="pageid" value="1">
  <INPUT type="submit" name="submit_button" value ="Page 1">
</FORM>

<FORM method="POST" action="process.php" name="form2">
  <INPUT type="HIDDEN" name="pageid" value="2">
  <INPUT type="submit" name="submit_button" value ="Page 2">
</FORM>

<FORM method="POST" action="process.php" name="form3">
  <INPUT type="HIDDEN" name="pageid" value="3">
  <INPUT type="submit" name="submit_button" value ="Page 3">
</FORM>



and so on. This displays a button instead of link, but you can use CSS to make the button look like a link.


Would it not be more efficient to have one form and give each submit button a different name? Less HTML at least.
JayBee
mathiaus wrote:
kv wrote:
I don't think lot of people turn off javascripts. Probably only those who have really sensitive data on the systems (like bank account data, etc). However, if you still want to do it without using javascript, here is the way.
Code:

<FORM method="POST" action="process.php" name="form1">
  <INPUT type="HIDDEN" name="pageid" value="1">
  <INPUT type="submit" name="submit_button" value ="Page 1">
</FORM>

<FORM method="POST" action="process.php" name="form2">
  <INPUT type="HIDDEN" name="pageid" value="2">
  <INPUT type="submit" name="submit_button" value ="Page 2">
</FORM>

<FORM method="POST" action="process.php" name="form3">
  <INPUT type="HIDDEN" name="pageid" value="3">
  <INPUT type="submit" name="submit_button" value ="Page 3">
</FORM>



and so on. This displays a button instead of link, but you can use CSS to make the button look like a link.


Would it not be more efficient to have one form and give each submit button a different name? Less HTML at least.


If you will use only
Code:
<input type="submit" name="pageid" value="3">

it's OK but the button will have caption "3" and you can't change it Wink
(of course you can but it will not send pageid=3 but for example pageid=Page%203)

but
Code:
<button type="submit" name="pageid" value="3">Page 3</button>

look nice, but this wil NOT work with IE6, because when you have more submit buttons in one form, IE6 will send all these buttons and you are not able to determine, which was clicked.
mathiaus
I meant like ...
Code:
<input type="submit" name="3" value="Page 3" />
<input type="submit" name="about_us" value="About us" />
<input type="submit" name="disc" value="Disclaimer" />


and then checking which exists
Code:
if(isset($_POST['3'])) {
//page 3 stuff
} elseif(isset($_POST['about_us'])) {
// about us page
}


Though I still don't undertand why anyone would want to do this so I don't know if its suitable or not.
MrBlueSky
ninjakannon wrote:
Oh that's simple! Thanks a lot. Very Happy

One thing; I want my website to be usable by as many people as possible - of course including those who have JavaScript turned off.
I've never looked at the options concerning JavaScript in detail, but when I was using a computer with JavaScript turned off, some still worked. Would links using the method you have shown above still work if JavaScript was turned off? Or was that just an odd case?

Also, do you know whether a large number of people turn off JavaScript? Or would the large majority of people visiting my website still be able to use the hyperlinks?


To make things work for people who have turned off Javascript I think JayBee's idea is good: creating a kind of wrapper or workaround with PHP.

There are people who have turned of Javascript. Most of them are what you could call 'experienced users', mostly using Firefox and/or Linux. They only turn on Javascript on for sites they trust. You can either put a message on your site requesting them to enable Javascript and hope they do or use a non-javascript solution like JayBee suggested.
ninjakannon
Wow, I've got more of a response than I was expecting to this! Very Happy

I've decided to use the first method Jaybee initially suggested because it's simple and will work effectively. The main reason I wanted to use POST variables is because it makes the url link look tidier. But as most people with Javascript turned off are 'experienced' users then they wouldn't be put off or confused by URLs with GET variables in.

Thanks for the help guys, I hope to implement this soonish, hopefully less than a week in fact. Smile
mathiaus
ninjakannon wrote:
The main reason I wanted to use POST variables is because it makes the url link look tidier. But as most people with Javascript turned off are 'experienced' users then they wouldn't be put off or confused by URLs with GET variables in.

That is what mod_rewrite is for. Using post means that all your pages will have the same URL. As a result, no-one will be able to link your content, so won't bother (less backlinks), search engines will have problems with your page and things like sitescore won't work on your site.
Any chance of changing your mind?
dayveday
I know what you're saying about making URLs looking neater, but that's definitely not enough of a reason in my opinion. It will probably annoy the hell out of your visitors, as everytime they want to move back they'll get one of the "This used post, are you sure? Blah blah" boxes. And, as mathiaus said, it'll will really confuse the poor search engines.

I'm only new to mod_rewrite, but I'd already swear by it. Give that a look at instead. You can use is so that things that are GETs (?section=8588&page=contact) look like normal urls (/8558/contact).
ninjakannon
Ah, I hadn't considered that. Yes, mathiaus, I'll definitely reconsider with regards to this.

I've never used mod_rewrite before so I'll need you to explain how I implement it, if you don't mind. It looks like a good solution this I suppose. One thing though, it will replace the get variables with a 'normal URL' structure, but if someone tries to use this same structured URL to visit my site, will it still work?
JayBee
ninjakannon wrote:
I've decided to use the first method Jaybee initially suggested...

I didn't suggested any working method. Very Happy


I use something like this. There are better rules. You can rewrite your url directly from /233/contact to ?pageid=233&section=contact but when you want more parameters, you must change .htaccess file = relearn how to do it after months.

.htaccess
Code:
RewriteEngine on
# put here your web root direcotry
# if you use your own domain name like http://your.server.net/
# leave it as is
RewriteBase /

# if the requested file
RewriteCond %{REQUEST_FILENAME} !-f
# or the requested directory doesn't exists
RewriteCond %{REQUEST_FILENAME} !-d
# call rewrite.php insted of showing 404 error message
RewriteRule ^(.*) rewrite.php


rewrite.php
Code:
$params = explode("/",$_SERVER['REQUEST_URI']);
/* if you call your site with
 * http://your.server.net/12342/contact
 * it will expand this query to array("12342","contact");
 * so use it as you like
 */

// for example
$_GET["pageid"]=$params[0];
$_GET["section"]=$params[1];
require_once("index.php"); // yes, the old unchenged index.php


index.php
Code:
// extended feature - prevent accessing page with url like
// http://your.serve.net/?pageid=12123&section=contact
// this is only explanation how to do it, I didn't test this exact code :-[
// but I use something similar
if(strpos($_SERVER["SCRIPT_NAME"], "rewrite.php")!==false
  && $_SERVER['REQUEST_URI']!="") {
  header("location: http://your.serve.net/".$_GET["pageid"]."/".$_GET["section"]);
}
// continue normal index.php
ninjakannon
JayBee wrote:
ninjakannon wrote:
I've decided to use the first method Jaybee initially suggested...

I didn't suggested any working method. Very Happy


I was talking about this:
JayBee wrote:
you can use
Code:
<a href="non-js-page.php?pageid=1" onclick="goto(1); return false;">Page 1</a>


Anyhow, thanks for that. I hope to get around to trying this in less than a week.

Again, thanks a lot - all of you. Very Happy
Related topics
HTML Tutorials
xmlhttprequest
CGI FormMail Script Part II
Help, I can't post an HTML signature!!!
AJAX tutorial [2nd part now updated]
xHTML what the heck
Can I have attachments added to forms?
30 $FRIH to fix my script!!
Need help with a HTML / PHP question.
Login System Tutorial Part 1 Creating Registration
Is there any way to submit post data with php?
50fri$ Please help me with curl.
Is there a simple way to convert a DOM back to HTML?
How to achieve 2 actions on html form "submit"
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.