Click to See Complete Forum and Search --> : Php?


Jonathan
07-05-2003, 06:49 PM
Can PHP make forms?

Jona
07-06-2003, 12:03 AM
Um, not sure what you mean by that. It can process them, but (X)HTML is the only way to make forms. But you can write HTML via PHP, like this:


echo ('
<form action="process.php" method="POST" name="processForm"><div>
<input type="text" name="fieldName"><br>
<input type="submit">
</div></form>
');


[J]ona

Jonathan
07-06-2003, 12:04 AM
does it actually send? not go to the mailto: command?

Jona
07-06-2003, 12:11 AM
Originally posted by Jonathan
does it actually send? not go to the mailto: command?

Actually, it sends but isn't processed. What you're asking for is a PHP script that processes forms. Here's an example.


<?PHP
if(!isset($_GET["node"]) || $_GET["node"] != "process"){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head><title>Form Processing in PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=8859-1">
</head>
<body>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>?node=process" method="POST"><div>
<input type="text" name="fieldName" value="Enter anything you want!"><br>
<input type="submit">
</div></form>
</body></html>
<?
} else {
mail("you@yoursite.com", "Subject - Email from your site", "Someone submitted the form on your site. They put in this:\n\n".$_POST["fieldName"], "From: Your_site.com");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head><title>Form Processing in PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=8859-1">
</head>
<body>
<p>You emailed:</p>
<p><? echo $_POST["fieldName"]; ?></p>
</body></html>
<? } ?>


[J]ona

Jonathan
07-06-2003, 12:13 AM
re-worded...

If I were to put a php coding on my site for a form, would I be able to see the outcome of the form in my e-mail, if so... what would it look like?

Jona
07-06-2003, 12:18 AM
Originally posted by Jonathan
re-worded...

If I were to put a php coding on my site for a form, would I be able to see the outcome of the form in my e-mail, if so... what would it look like?

I've edited my post.

[J]ona

Jonathan
07-06-2003, 12:21 AM
I am soryr, but I am new to all this PHP stuff... I don't ahve a clue what you are saying... :)

Jona
07-06-2003, 12:24 AM
If you copy and paste everything I've posted (and edit the you@address.com part in the mail() function), and save it as "anything.php" and run it on your site, you'll see what the code does.

[J]ona

pyro
07-06-2003, 05:58 PM
You might want to use something like this: http://forums.webdeveloper.com/showthread.php?s=&threadid=9543#post48748

Then, once you have that set up, just set up your form tag something like this:

<form action="mailer.php" method="post">

where mailer.php is the name you gave the script from the link above.

Jonathan
07-06-2003, 06:22 PM
3 things...


if(!isset($_GET["node"]) || $_GET["node"] != "process"){

What does this mean?

How would I fit other stuff like a checkbox, textarea, etc... what would I have to do from this:

mail("webmaster@crosspoint.org", "Subject - Feedback", "Someone submitted the form on your site. They put in this:\n\n".$_POST["fieldName"], "From: crosspoint.org");

Would I just type that echo $_POST[fieldname] again?


AND how come there are 3 pages on one page?

----This is really helping... just to let you know

PYRO---
How do you work your php script?

pyro
07-06-2003, 06:28 PM
Originally posted by Jonathan
PYRO---
How do you work your php script? Please ask a more specific question. I don't know what part you do not understand. What you do is take the script I linked to, change the info in the top to be what you want, save it as mailer.php (or anything with a .php extention) and link to it in your forms action.

Jona
07-06-2003, 06:29 PM
The first if statement is a test to see which command to execute: display the form, or process the form? If the form hasn't been submitted, then you can't process it, but if the form has been submitted, then you can. So, instead of checking to see if(isset($_POST["fieldName"]), we check to see if the query string is nothing. If it is, display the HTML form, if it's not, process the form. That is why I have the action="thephpPage.php?node=process" on it. If I took that off, it wouldn't process the PHP, it'd just show the form again. This also explains the, "three pages on one page." (Which, actually, it's two pages. ;))

Adding form elements is done in the same way the first one is done. You add HTML elements to the first part of the PHP code--the one that displays the form--and you name them whatever you like. Then you call them with $_POST["nameOfTheField"] and you can add them to your email and echo them to the next page at will. (This also answers your "third" question.)

[J]ona

Jonathan
07-06-2003, 06:31 PM
How do I adjust what type of form items are on the form, or does it matter... Can I put as many items on there as I would like?

Jona
07-06-2003, 06:33 PM
You can put as many form items as you like in there, and it will work. You can put any type of input in there, and it will work.

[J]ona

Jonathan
07-06-2003, 06:34 PM
So all I have to do is put $_POST["nameOfTheField"] to make it show up on my form in my e-mail?

Jonathan
07-06-2003, 06:35 PM
What about making it say somehting like this on the e-mail

Name: The_persons_name
Address: and so on

Jona
07-06-2003, 06:35 PM
Yes, but when I said, "nameOfTheField" I meant it as in, "put the name of the field here."

[J]ona

Jonathan
07-06-2003, 06:37 PM
Sorry, but I am finally getting this... what does this mean

<?PHP echo $_SERVER['PHP_SELF']; ?>

and do I have to change it?

Jona
07-06-2003, 06:39 PM
It shows the file that it's on. So if the file was named, "mailer.php" it'd echo: mailer.php. It's there so that you don't have to change it depending on the name of your file.

[J]ona

Jonathan
07-06-2003, 09:04 PM
what does the /n/n thing mean?

Jona
07-06-2003, 09:05 PM
Same thing as <br> but in text format, instead of HTML format. (Try this JavaScript: alert("Hello,\nWorld!");.)

[J]ona

Jonathan
07-06-2003, 09:09 PM
Is this right?

mail("webmaster@crosspoint.org", "Feedback--www.crosspoint.org", $_POST["name"] "submitted the form on your site.\n\n".$_POST["name"], "From: crosspoint.org");

and, do i need the "From:crosspoint.org" thing?
and, how do I add another post item, on the other side of the ";" or before that?

Jona
07-06-2003, 09:12 PM
Use the dot operator to append values. Here is what it should look like:


mail("webmaster@crosspoint.org", "Feedback--www.crosspoint.org", $_POST["name"]." submitted the form on your site.\n\n".$_POST["name"]."\n".$_POST["email"]);


[J]ona

pyro
07-06-2003, 09:12 PM
I would highly recommend using a script like the one I linked to above... It will easilly account for multiple fields, due to the foreach loop...

Jona
07-06-2003, 09:13 PM
Originally posted by pyro
I would highly recommend using a script like the one I linked to above... It will easilly account for multiple fields, due to the foreach loop...

Could save me the trouble, too. :D:D:D

[J]ona

Jonathan
07-06-2003, 09:15 PM
it this correct though?

mail("webmaster@crosspoint.org", "Feedback--www.crosspoint.org", $_POST["name"] "submitted the form on your site.\n\n".$_POST["name"], "From: crosspoint.org");

Jonathan
07-06-2003, 09:17 PM
soryr, didn't see the response

Jona
07-06-2003, 09:18 PM
Originally posted by Jonathan
it this correct though?

No, it is not.

[J]ona

Jonathan
07-06-2003, 09:22 PM
where do i put the html pyro?

pyro
07-06-2003, 09:29 PM
Once you set up the PHP script (ie. change the values to yours -- email address, etc), you just save it as mailer.php and then put something like this in your html page:

<form action="mailer.php" method="post">
First Name: <input type="text" name="first_name">
Last Name: <input type="text" name="last_name">
<input type="submit" name="submit" value="Submit">
</form>

[edit - For example, the script mailer.php doesn't have the HTML form in it. It just processes your form. Any page can have the form...]

Jonathan
07-06-2003, 09:34 PM
so I can point all my forms to that? cool... Thanks

pyro
07-06-2003, 09:36 PM
Yes you can -- yet another reason why that is the best way to go... :)

Jonathan
07-06-2003, 10:10 PM
Yes you can -- yet another reason why that is the best way to go...

lol.. good advertisement too...

Can I delete all the comments except for the copyright part?

pyro
07-06-2003, 10:13 PM
Yes. And, the only reason the copyright is there is to keep people from distributing it as their own...

Jonathan
07-06-2003, 10:15 PM
so what are you saying,

and, can I send this form to multiple addresses like this:

$to = "dude@jdk.com,someoneelse@somewhere.com"

pyro
07-06-2003, 10:27 PM
I'm saying that if you are using it for your own personal use, and aren't going to send the script to anyone else, you can remove the header copyright, as well.

To send to multiple addresses, you'll have to use another loop. Replace this line in the old script:

mail($to, $subject, $msg, $headers);with:

$addresses = array("one@domain.com","two@domain.com","three@domain.com"); #set the to: addresses. You can move this line up to where the rest of the variables are defined if you wish.
foreach ($addresses as $email) {
mail($email, $subject, $message, $headers);
}

Jonathan
07-06-2003, 10:52 PM
What am I dooing wrong?

if (document.form.name.value=="" && document.form.email.value=="")
{
alert("Please fill in your name and e-mail address.")
document.form.name.focus();return false
}

it says, return value out of function



and thanks for the php

Jonathan
07-07-2003, 01:38 AM
I have a new question...

http://forums.webdeveloper.com/showthread.php?s=&threadid=12350