Jump to content

Adding PHP parameters to a URL when page loads


moshina786

Recommended Posts

Hi All.

 

I have been really struggling with this for a while! When a URL loads I wish to load with parameters entered & those parameters entered on a page but cannot get my head round it. Any help will be much appretiated...

 

So when a user navigates to www.test.com/page.php I wish to to actually pull back www.test.com/page.php?term=&submit=Go

 

Any help will be really really appretiated.

Well you could use mod rewrite.

 

Or you could write the link with the variables in it (ie <a href="text.com/page.php?term=&submit=Go">link</a>) or use a header('Location: test.com/page.php?term=&submit=Go') to redirect (or javascript or meta tags if you'd prefer).

 

You could also use a form with a 'get' method that directs back to itself through the action part when a user submits it.

<form action="test.com/page.php" method="get">

The menu has the full link within it so that's ok, but if a user navigates to www.test.com/page.php the parameters arent added. In more details, the parameters are for a form to be submitted to show all results.

Is there such a thing as to submit a form on page load? Which in essence I suppose I am asking.

Yup:

 

<script type="javascript"> 
window.onload = function() {
document.myform.submit();
}
</script>

<form id='myform' name='myform' action='<?php echo $_SERVER['PHP_SELF']?>' method='get'><br />

and the form goes on....

 

Any ideas ?

You shouldn't use javascript to solve this problem anyway, I'm sorry but it doesn't make sense to do it. You should only use javascript to enhance user experience, not for something that's absolutely necessary.

 

Especially for something like this. Default values are the best way to go for a search page, it will function just as well as if the form was submitted. If you absolutely must have the parameters in the url, as I said before you should use a header at the very top of the page (before any html) to redirect the page if there's no parameters, or a javascript or meta redirect to do as much.

 

if (!isset($_GET['term'])) {
     header('Location: page?term=&submit=Go');
}

 

This will give you your default values as if the form was clicked on with no changes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.