Jump to content

getting data from mysql database to form fields (autofill in)


beaviss

Recommended Posts

Hi,

I have been battling with this for ages now, any help will be appreciated.

I am trying to create a page with about 10 form fields on it. I would like to have a drop down list of members, and on selecting one, the fields get filled out. Either from database or other method is there is.

Then these fields (now filled out) need to be placed in the correct places on a template and emailed in plain text to a specific address.

I sure the solution is very simple, but I am still very new to php, and this would really teach me lots.

I would like to be able to add new users info to the database, and if it isnt 2 hard even keep track of products they order with a date and remind me if before expiration.

many thanks
B
Link to comment
Share on other sites

Well, actually what you're asking is fairly advanced. But don't worry, it is all possible and I'll try my best to help you out. Firstly, you need a database structure. You'll need three tables, one for user info, one for product info and one for the user's products. The user table might have these columns:
id, fname, lname, email, password

Products:
id, name, category, price

User's products:
id, uid (id from the user table), pid (id from the products table), date, quantity

Then basically, once a user signs up, their information goes into the user table. You fill in the product table by yourself, with all your product info. Then when a user orders a product, their id goes into uid, the product id goes into pid, the date they ordered it goes into date and so on.

In order to do exactly what you want, you'd need to use Ajax which is a mixture of PHP and javascript. I can show you how to do that if you want but it may be difficult. What you're asking doesn't really require that, as you can just get all the values from the database and email them with PHP without having to use a form (well maybe to select the user).

You said you're new to PHP but I don't know how new. So if you'd like me to walk you through each bit of code needed, I can. If you're fine with what I've said above then that's ok too.
Link to comment
Share on other sites

Hi Fyorl,

Thanks for the quick response.

What you say sounds great.

Let me explain exactly what I [i]need[/i] to do, the rest is not all that important.

We have a script which does all domain registrations for us, except co.za

On the form I just need to enter the domain I need to register and select the user (would be handy, if it could open a popup with the info so I know I have the correct user.)

I need to enter the users details in so each time I register a new domain, it only takes me a sec instead of having to type it all out manually in the current html form.

It would be nice it I could store the domains I register, and if the system could email the user to alert to expiration.

I honestly dont need anything fancier than that, I really would love something as above to make life simpler.

The coza registry has a set form which needs to be emailed to them in plain text with these fields filled out.

Does this make it any easier ?

Would you be able to help me with the database (a few guidlines) I always seem to mess up the primary keys.

Thanks a ton !
B
Link to comment
Share on other sites

Uh huh, that's all completely doable. The only thing is the popup. If you desperately want to have the popup come up right after you select the user from the drop-down menu, it can be done with Ajax. Otherwise, you can just select the user from the menu, and the domain name, click the submit button and the next page will have all the forms filled out for you. See what I'm getting at here? If you want instant form filled-outness you need to use ajax, if you don't mind clicking the submit button then PHP can do it alone.

As for the tables, I'll walk you through setting them up properly. First I need to know what info you're collecting from the users (first name, last name, email). We'll do one table at a time...
Link to comment
Share on other sites

ok, so if I dont use a popup, the next page will have a filled in form which I will have to submit again ? so if I selected the wrong user, I can click back and do over ?

There are about 20 fields. name,surname,postal adress,streetaddres,homeph,workph,mobile,email,billingemail,admincontact,ph,ph,fax,nameserver1,ip1,nameserver2,ip2 give or take.

Users dont need to register, if there are users I will want to create them, and they shouldnt be able to see the list, just use the info in the database that pertains to them. but at this point users are not my worry.

please read pm

Thanks.
Link to comment
Share on other sites

OK, I answered your PM and forgot to mention that as we are a design company, we could do the whole thing and make it look very nice in the process. Plus, I would be able to use Ajax fully. However, that's just aesthetics and if you want the job done, I can help you right now with the code. You'd need something like this (HTML):
[code]
<form name="form1" action="script.php" method="post">
<input type="text" name="domain" />
<input type="text" name="username/id" />
<input type="submit" name="submit" value="Go!" />
</form>
[/code]
Then in script.php, you'd have something like:
[code]
<?php
if(isset($_REQUEST['submit'])) // Check the script is receiving data from the form
{
$domain = $_REQUEST['domain']; // Put the data into nicer variables
$user = $_REQUEST['username'];
$sql = mysql_query("SELECT * FROM `table` WHERE `username`='$user'"); // Get all the user info from the database
$info = mysql_fetch_array($sql); // Put it in an array
}
?>
<!-- More HTML -->
<form name="form2" action="register.php" method="post">
<input type="text" name="domain" value="<?=$domain ?>" />
<input type="text" name="username" value="<?=$user ?>" />
<input type="text" name="fname" value="<?=$info['fname'] ?> />
<input type="text" name="address" value="<?=$info['address'] ?> />
<input type="submit" name="register" value="Register" />
</form>
[/code]
You get the idea. However, if all the register script does is get those values, put them in a template and email them. You can skip that last bit of HTML and do it all in PHP. Once PHP has all the values from the database, it can put them in the template straight away. But if you want to be able to check you've got the right user, you'll need to use the above.
Link to comment
Share on other sites

hi Fyorl,

I think I can make sense of that.

I just dont understand how the html part ends up in a finished template.

Do I have to put the plain text in a container " ' ; (something like that) or just work the html inbetween the template.

Maybe doing it in php is the easiest. How do I go about mailing. Would I be extracting the email address from the database also as the From: header and To: will always be the same address. (possibly I can include myself here to make sure I sent the correct details :) )

Thanks,
B
Link to comment
Share on other sites

To get them into the template you'd use another php script like script.php to get the values from the form and then arrange the values in whatever format's needed. To send them you'd use the mail() function but it's been a while since I used that so I'd have to look it up again. Yes, you could pull the email addresses out of the database but the generated form that you use to check you've got the right person should already have the emails address in so you can just use that. The From header will always be your address but the To header will change depending on what user it is.

If you need example code:
[code]
if(isset($_REQUEST['register'])) // Same as before
{
$keys = array_keys($_REQUEST);
foreach($keys as $key)
{
$$key = $_REQUEST[$key]; // No the $$ is not a mistake, it's a variable variable name.
}
/*The whole foreach thing is just a way of getting the data into easier to use variables. For instance, it turns $_REQUEST['domain'] into $domain and $_REQUEST['fname'] into $fname*/

$to = $email;
$from = 'your@email.address';
$subject = 'Domain Registration';
$msg = "| First Name | Surname | Domain | Phone No\n\t$fname\t$lname\t$domain\t$phone"; // \n is a new line, \t is a tab, if they want the data laid out in a table or something

mail(
$to,
$subject,
$msg,
"From: $from\r\n
Reply-To: $from\r\n
X-mailer: PHP v4.3.1"
); // The extra headers aren't needed.
}
[/code]
Link to comment
Share on other sites

hi,

Thanks, this is very helpful, esp the tips.

Last question.

The form has to be sent in its original form.

ie

This is the xxxversion and blah blah blah

Domain:
Renew [R] Update[U] Delete[D]:
Name of registrant:
Street Address:

and the machine reads the info after :

How do I get them in there and send it in plain text ?

Thanks,

B.
Link to comment
Share on other sites

Using my code from above:
[code]
$to = 'registration@domains.co.za';
$from = 'your@email.address';
$subject = 'Domain Registration';
$msg = "Domain: $domain\n
Renew [R] Update[U] Delete[D]: R\n
Name of registrant: $fname $lname\n
Street Address:$addr1\n$addr2\n$addr3\n$addr4";

mail(
$to,
$subject,
$msg,
"From: $from\r\n
Reply-To: $from\r\n
X-mailer: PHP v4.3.1"
); // The extra headers aren't needed.
[/code]

The address could be done a bit differently. Instead of:
[code]
<form>
<input type="text" name="addr1" />
<input type="text" name="addr2" />
<input type="text" name="addr3" />
<input type="text" name="addr4" />
</form>
[/code]
You could use:
[code]
<form>
<input type="text" name="addr[]" />
<input type="text" name="addr[]" />
<input type="text" name="addr[]" />
<input type="text" name="addr[]" />
</form>
[/code]
and then in your PHP script you would use:
[code]
$addr = $_REQUEST['addr'];
/* But instead of $addr being one value, it's actually an array like:
$addr[0] = the first box
$addr[1] = the second box
and so on
So modifying the original code, we could use:*/

$msg = "Street Address:\n$addr[0]\n$addr[1]\n$addr[2]\n$addr[3]";

// or even:

$msg = "Street Address:\n";
foreach($addr as $ad)
{
$msg .= $ad . "\n";
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.