Forum Moderators: coopster

Message Too Old, No Replies

Submit information before page is load.

         

puckparches

9:31 pm on Sep 3, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello

I have a page with the some user information. The function in the pages gets the username and returns some result from a database.The page uses a template.

When I link to the user page I used:

[........]

That link works perfect, the user gets his information with no problem.

But if the user reaches the page directly.

[........]

Then the user gets the first rows from the database.

I think the problem is the the page is displaying the information before it reads who is the user in the page.

I found this script to submit a form on page load but is not working

<head>

<script type="text/javascript" language="javascript">
function submitform(){
document.getElementById('myForm').submit();
}
</script>
</head>
<body>
<body onload="submitform()">

<form name="myForm" action="mypage.php" method="post" target="_blank">
<input type="hidden" name="username" value="username">
</form>

<script type="text/javascript" language="javascript">
document.getElementById('myForm').submit();
</script>

</body>

How can I submit the form before is the page is loaded?

Thanks you!

islandlizard

3:01 am on Sep 4, 2006 (gmt 0)

10+ Year Member



You can't submit a form before the form has loaded, as this makes no sense.

The script you have will only ever submit 'username', which is probably not what you want.

Ideally, you need to use PHP to check if a username was in the URL. If it is, then you can display the info for that username. If not, you can display the form for the visitor to enter the username they want.

You just need an if/else statement to check if the URL contained the?username=[username] parameter, i.e.

<?php

if (isset($_GET['username'])){

//username exists, so run query

} else {

// no username was in the url
// show visitor the form

}
?>

That way, you show a result if a username exists, but if it doesn't the form will be shown instead.

As PHP is handling the request, it will do all this before anything is sent to the server, allowing you to catch a missing username before anything is sent to the server.

HTH

Ben

inquireorenquire

4:44 am on Sep 4, 2006 (gmt 0)

10+ Year Member



You might also want to read up about PHP sessions - they will enable you to remember the user from page-to-page without having to submit a form.

puckparches

5:09 am on Sep 4, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you very much islandlizard and inquireorenquire

My pages do have session, but I'm not sure why is not loading the page user name before the information is presented. I'll try what islandlizard suggested.

Thank you!