moshina786 Posted February 18, 2011 Share Posted February 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/ Share on other sites More sharing options...
RestlessThoughts Posted February 18, 2011 Share Posted February 18, 2011 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"> Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176445 Share on other sites More sharing options...
moshina786 Posted February 18, 2011 Author Share Posted February 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176455 Share on other sites More sharing options...
beegro Posted February 18, 2011 Share Posted February 18, 2011 You can do it with javascript window.onload = function() { // where my_form is the name of your form element document.my_form.submit(); } Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176468 Share on other sites More sharing options...
RestlessThoughts Posted February 18, 2011 Share Posted February 18, 2011 You could also have default values. if (!isset($_GET['term']) OR trim($_GET['term']) == '') { $_GET['term'] = 'Your Default'; } Or alternatively redirect back to the form page if there are no get values. Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176474 Share on other sites More sharing options...
moshina786 Posted February 18, 2011 Author Share Posted February 18, 2011 Hi Beegro, I entered this <script type="javascript"> window.onload = function() { document.myform.submit(); } </script> However it doesnt seem to do anything! Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176477 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 is there a form with id/name myform? <form id='myform' name='myform' action='' method='get'> Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176480 Share on other sites More sharing options...
moshina786 Posted February 18, 2011 Author Share Posted February 18, 2011 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176485 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 any errors reported in javascript console? maybe try body.onload instead of window.onload Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176500 Share on other sites More sharing options...
moshina786 Posted February 18, 2011 Author Share Posted February 18, 2011 Nothing in the console....and same result when replacing with body! Pulling my hair out with this one!! Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1176505 Share on other sites More sharing options...
RestlessThoughts Posted February 21, 2011 Share Posted February 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228127-adding-php-parameters-to-a-url-when-page-loads/#findComment-1177722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.