New to PHP and very confused...

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
Okay, so I was trying to get a form to send the results to me via email. Someone else had the same issue and was told that a php script would solve the issue. Fine, great... but I don't get how to use it in my situation. I wrote a script and called it HandleForm.php my trusty, dusty PHP book (Visual Quickstart Guide - PHP for the World Wide Web) shows how to do this. I uploaded the page with the form and the corrected action as well as the php page. But now... it doesn't work. I was getting the php page with no input but now, I get nothing.

I saw elsewhere where a member talked about phpMyAdmin. I went there on the cPanel, but it's all greek to me. Where can I get help with php and this phpMyAdmin application?

This is what I wrote. Can there only be one string/variable on a line? Is that part of my problem?

PHP:
<HTML>
<HEAD>
<TITLE>Form Resluts Page</TITLE>
<BODY leftmargin="10%" rightmargin="20%" bottommargin="25">
<?php 
/* This page handles the received data from the form "bpaforms.html". */
print "Thank you for submitting your information, $First.<BR>\n";
print "We show your address as $Address \n";
print "in $City, $State $ZIP.<br>\n";
print "Your phone number is $phone \n";
print "and your e-mail address is $email.<BR>\n";
print "You have asked us to send you information about our schools for your child(ren),<BR>
$Child1, age $Age1 ($Sex1),  $Child2, age $Age2 ($Sex2),  $Child3, age $Age3 ($Sex3)<BR>\n";
?>
</BODY>
</HTML>

To see it in action (or not), the page is here.
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
PHP:
<HTML>
<HEAD>
<TITLE>Form Resluts Page</TITLE>
<BODY leftmargin="10%" rightmargin="20%" bottommargin="25">
<?php 
/* This page handles the received data from the form "bpaforms.html". */
$First = $_POST['First'];
$Last = $_POST['Last'];
$Address = $_POST['Address'];
$City = $_POST['city'];
$State = $_POST['State'];
$ZIP = $_POST['ZIP'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$Child1 = $_POST['Child1'];
$Age1 = $_POST['Age1'];
$Sex1 = $_POST['Sex1'];
$Child2 = $_POST['Child2'];
$Age2 = $_POST['Age2'];
$Sex2 = $_POST['Sex2'];
$Child3 = $_POST['Child3'];
$Age3 = $_POST['Age3'];
$Sex3 = $_POST['Sex3'];

print "Thank you for submitting your information, $First.<BR>\n";
print "We show your address as $Address \n";
print "in $City, $State $ZIP.<br>\n";
print "Your phone number is $phone \n";
print "and your e-mail address is $email.<BR>\n";
print "You have asked us to send you information about our schools for your child(ren),<BR>
$Child1, age $Age1 ($Sex1),  $Child2, age $Age2 ($Sex2),  $Child3, age $Age3 ($Sex3)<BR>\n";
?>
</BODY>
</HTML>

depending on your form, whether the method says post or get, you would use global varialbes to get data from it.

if it's post, you'll do $_POST['input_name'] to get the data from the input field named input_name.

if you used $_GET['act'], you'll get data from the url, so index.php?act=blah&p=2 would make $_GET['act']'s value "blah" and $_GET['p'] equal to "2".

You can echo/print multiple variabes in one line. Above, what you did as far as variables is right, I just don't think you retrieved any information from the previous field.

-xP
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
I can't remember which setting did this, but there's a setting in php which makes all post/get data a global variable. This is a large security risk. That's why you should always disable that setting!
This is also why your script is not working.
As xP already pointed out, you could use $_GET or $_POST (depending on your form method). I just wanted to explain a little why you/your php book failes there, and also mention $_REQUEST, which is $_GET and $_POST (and $_COOKIE) at the same time. But it's better to use the specific global instead of request, if you already know which one you should use.

- Marshian
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
Awesome! Thank you so much!! I couldn't understand how the script could be as small as I had it and still work. I had a feeling I was missing something major. Thanks for pointing it out and for the information. Hopefully it will help in the long run.


Also, how would I use this to send an email? I'd only need to change the action, right?
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
np, always here to help

Register globals is a HUGE security risk as mentioned above, which is why it's no longer On by default.
PHP:
<?php
if ($a==1) $username="Henry";
$res = mysql_query("SELECT uid, password FROM user WHERE username='$username'");
?>
with registered globals on, if the url is
?a=2&username=' OR ''=''
then that'll transform the query into
"SELECT uid, password FROM user WHERE username='' OR ''=''
and since '' == '', then it'll return all usernames and all passwords
devil-smiley-019.gif
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Just a note, Register Globals isn't as big of a security risk as is commonly said. If you code with security in mind (as you should), you probably wouldn't run into any issues with it. However, since it promotes bad habits and can be misused by beginners, it's generally not a good idea to enable it.
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
Just a note, Register Globals isn't as big of a security risk as is commonly said. If you code with security in mind (as you should), you probably wouldn't run into any issues with it. However, since it promotes bad habits and can be misused by beginners, it's generally not a good idea to enable it.

Thanks... I still don't understand what it is, so there's no worry of enabling it. :dunno:
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Say you got a query part of an url ?var1=value , you can access variable var1 as $_GET["var1"]. If you have Register Globals on, you can also access it as $var1 .
 
Last edited:

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
OKay, y'all... I think this issue is resolved! Thanks SO very much for all the help. I am off to play in the land of php and see what else I can find.



Please CLOSE this thread. Thanks!
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Glad you got it working and learned from here.

You can close your own topics in this section (when you post, towards the bottom of the page, there's a checkbox to close the topic). Also, please give credit where due (as rep) ;)

Until next time,
-xP
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
A wealth of knowledge you are, xP! Thanks! ;D However, I am not seeing the said check-box.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Thanks, I've been programming w/ php for a few years off and on. Add to it that my site is completely built around PHP/mySQL (very little static content)...

Are you quick replying? Go advanced and you should see it...
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
I am still unable to locate the option to close this thread. I've seen it in other threads, but but not this one. Would someone please close this for me? Thank you!!
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
Okay... I am glad that I didn't close this thread. I am having trouble again! :(

I have 2 php pages for a form on one of my pages. When I try to test the page and submit the info, I get an error message that says, "Parse error: syntax error, unexpected $end in /home/bpzkidz/public_html/contact.php on line 34".

Here is my code:
PHP:
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"Message"} = "Message";

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: noreply@bpakidz.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us about child care. Somebody will get back to you as soon as possible, usualy within three business days. If you have any more questions, please consult our website at www.bpakidz.com.  If you do not hear from us within a week, please call Michelle @ 813-281-9388. ";

if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
if($phone == '') {print "You have not entered a phone number, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://bpakidz.pcriot.com/Thanks.html" );}
else
{print "We encountered an error sending your mail, please notify webmaster@bpakidz.com"; }
}
}
?>

PHP:
<HTML>

<HEAD>
<TITLE>Form Results Page</TITLE>
</HEAD>

<BODY leftmargin="10%" rightmargin="20%" bottommargin="25">

<?php 
/* This page handles the received data from the form "bpaforms.html". */

    $to = "beachparkacademy@gmail.com";
    $subject = "BPA Information Request";
    $email = $_POST['email'] ;
    $First = $_POST['First'];
    $Last = $_POST['Last'];
    $Address = $_POST['Address'];
    $City = $_POST['city'];
    $State = $_POST['State'];
    $ZIP = $_POST['ZIP'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $Location = $_POST['Location'];
    $headers = "From: $email"; 
    $sent = mail($to, $subject, $headers, $Location ) ;

    if($sent)
    {print "Your mail was sent successfully"; }
    else
    {print "We encountered an error sending your mail"; } 
?>
</BODY>
</HTML>

If anyone can help me to figure out where I messed up, I'd truly appreciate it!
 

scopey

New Member
Messages
62
Reaction score
0
Points
0
Need another } before the ?> of your first document. You should really think about formatting your work with tab and putting }s on new lines and such... Makes it easier to see mistakes.

Also, I'm a bit confused with that $fields array definition you have... The {s and }s should be [s and ]s.
 

bpakidz

New Member
Messages
36
Reaction score
0
Points
0
Thanks!

When you say to "tab" my work, what would be a good way to do this? Where are the best examples of php online?
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I don't really understand global variables and don't use them..

My script is simple.

First, I convert the text from the form to a simpler variable for ease..

PHP:
<?php
 $firstname= $_POST['firstnametextfield1'];
 $lastname=$_POST['lastnametextfield2'];
 $contactemail=$_POST['email'];
?>

etc.

The e-mail bit is like this.

PHP:
<?php
   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   $headers .= "Content-Transfer-Encoding: 7bit\r\n"; 
   $headers .= 'From: you@yoursite.com(you@yoursite.com)' . "\r\n";
   $headers .= 'Reply-To: you@yoursite.com' . "\r\n";
   $headers .= 'Return-Path:'.$contactemail. "\r\n";
   $headers .= 'X-Sender: '.$contactemail. "\r\n";
   $headers .= 'X-Mailers: PHP /'.phpversion() . "\r\n";
   $subject = "Whatever Subject";
 
   $message  = ' (split off below)  ';
 
  ini_set(sendmail_from,$contactemail);
  if (@mail(''.$firstname.' '.$lastname.'<'.$contactemail.'>',stripslashes($subject),stripslashes($message),stripslashes($headers)))
  {
    echo ('
 <p>E-mail successfully sent to '. $contactemail . '</p>
 ');
  }
  else
  {
    echo ('
 <p>Error! The E-mail has failed to send to ' . $contactemail . '. Please try again.</p>
 ');
  }
 
  ini_restore( sendmail_from );
?>

The content of the e-mail message itself (to replace the bit that is split off) is standard html but with a twist. Note that it is encased within the script with single quotation marks.

PHP:
    <html>
  <body>
  <font size="2" face="Arial">
  <p>Dear '.$firstname.' '.$lastname.',</p>
  <p>This is a test e-mail.</p>
  <p><em>
        Do not reply to this e-mail. If you have received this e-mail in error, please ignore it.
  </em></p>
  </font>
  </body>
</html>

All of this is standard html except where you want to add data from a variable.

Where you want to insert data (red text variables), you have to stop the html, echo the value, then continue the html.

To do this, you stop the html using a ' (single quote mark) then a . (stop) to add the variable to the script, then put the variable $whatever. To continue the html, you add a . (stop) then another ' (single quote) and then continue the html as you would before.

Using this technique, you can format highly complex html and insert variable values whereever you want. The only limitation is that you need to be careful about the use of quote marks.
 

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
I personally prefer using
echo "Text <b>text $variable</b><br>More text.";
most of the time. But that's not really the point...

As for indentation, I'd like to see a good formal example as well.
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Or you can use:
PHP:
$html=<<<EOF
<p>Hello $name. You are $age years old</p>
<p>More html stuff with $variable</p>
EOF;  //this MUST be without indentation, first on the line

As for indentation, everyone seems to do it differently, but if you just make your {} match, it really helps. This is how I do it:

PHP:
foreach($array as $k=>$v) {
  for ($i=0;$i<5;$i++) {
     if ($i==$k) {
        echo "$i is $k";
     } else {
        echo "You lose!";
     }
  }
}
 

Salvatos

Member
Prime Account
Messages
562
Reaction score
1
Points
18
Same for me, I've come to feel it's the most efficient way. I've also seen people indenting HTML, but personally I just do it for stuff like this:

Code:
<table>
 <tr>
  <td>
  Content
  </td>
 </tr>
 <tr>
  <td>
  <table>
   <tr>
    <td>
    New table
    </td>
   </tr>
  </table>
  </td>
 </tr>
</table
So it's easier to find a particular group of cells. Is there supposed to be any "regulation" on HTML indentation as well?
 
Top