Jump to content


Photo

specific questions


  • Please log in to reply
12 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 05:28 PM

ok maybe my other post was too run down, and not clear enough to get answers from, so I decided to repost with some clear questions. I tried running one single post forever, with all the questions I ever had, but it may not work. I am working on a process, I am trying to master the art of getting form information sent via email. Then I will work on simple validation with both php and javascript. Then intermediate and advanced forms of validation. I have been working with getting the information sent via email but a few things are throwing me off.
1. The main thing is the fact of setting elements as an array. I have all the elements working right, even the drop down select menu, and radio buttons work. But the checkboxes, it only registers one choice, I know to set the names with name[] and then set each value as something. I was told how to pass it into an array and redisplay the information on the screen, but I need to set it up as an array and get it to email me the answers that were chosen, I even learned how to format the information so the email looks nice and everything< i can't avoid this element as when I start doing jobs of course most forms I run across will have checkboxes.
2. Another important thing I don't understand is this, I use the following code to process my form, and it worked fine except for the check boxes
<?php
$to = "businessman332211@hotmail.com";
$subject = "freelance businessman!";
$body = "
First Name:$firstname
Last Name:$lastname
Age: $age
Location: $location
Favorites: $favorites
Choices: $choices
Username: $username
Verify Username: $verifyusername
Password: $password
Verify Password: $verifypassword";
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }
?>
ok that got it processed alright and emialed to me in the correct format, I can do this with anyform now taht I mastered this part of php.
but what I don't get is some people do things like this.
<?php
    $_GET['Languages'] = implode(', ', $_GET['Languages']);
    $_GET['Story'] = str_replace("\n", "<BR />", $_GET['Story']);

    print "Your name: {$_GET['Name']}<BR />";
    print "Your password: {$_GET['Password']}<BR />";
    print "Your age: {$_GET['Age']}<BR /><BR />";
    print "Your life story:<BR />{$_GET['Story']}<BR /><BR />";
    print "Your favourite sport: {$_GET['FaveSport']}<BR />";
    print "Languages you chose: {$_GET['Languages']}<BR />";
?> 
I know some of this is pointless, they have all these _GET things and _POST on some, but I don't get why I need that, when I send it in an email I DO NOT use those, and it sends the email, and still registers the variables, they are still accesible without using the get and post before each name, I don't udnerstand this because even people who tried to help me through here always had the _GET, _POST before each variable.
3. Another question I ahve is when I validate with php, should I do it in the external page that I emailed it in, or should I do it on the same page, the javascript I know on the same page, I will set something up, but right now I was concentrating on the emailing

Sorry to post again, but I only have those few things holding me back, after I finish working through that then I can move onto validation, but until I get those atleaast first 2 answers figured out I cannot move foward, and this is the only place I can ask at, and I looked everywhere, ALL the websites and tutorials show using _POST, and _GET in front of each one, these are the last 2 thigns I don't understand, and I have to know these before I move onto validation.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 April 2006 - 05:49 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]I know some of this is pointless, they have all these _GET things and _POST on some, but I don't get why I need that, when I send it in an email I DO NOT use those, and it sends the email, and still registers the variables, they are still accesible without using the get and post before each name, I don't udnerstand this because even people who tried to help me through here always had the _GET, _POST before each variable.[/quote]

This means you are running your server with "register_gobals" set to ON, so turn it to OFF in your php.ini file.

1 ) it's a securuty loophole to run with it ON
2 ) anything you develop for a client to run on their server won't work if they have set OFF ( and they probably will as that is default setting).
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Mortier

Mortier
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 April 2006 - 05:51 PM

Replying to question 2:

Well for the $_POST and $_GET thing, this is information send after the form.

You probably get your data, the variabeles from a form:

something like:
<?

<form method="post" action="">
  <table>
     <tr>
         <td><input type="text" name="first_name" ></td>
         <td><input type="submit" name="submit" ></td>
     </tr>
  </table>
</form>
?>

Well the information a user entered in the input area 'first_name' is stored in a $_POST['first_name'] if the form-method is POST. As in my example.

So POST is just form data.
People use that in their example because you want to e-mail form information.

GET is quite the same in use(apart from that it uses the URL to send info), there's loads of info on it.

Reply on question 3:

Doesn't matter for your script but sometimes you can understand your code better if you split it up in several parts.

I don't think I can answer your first question , I'm not that good in PHP myself, sorry

Well hope this helps

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 06:00 PM

ok finally something that really helped, I was worried about the register globals, and now I am for sure, I will figure out how and turn that off, then I will change that information and get it to work and ask a few more questions, I still need to figure out how to string that as a array so I can let the email send me the informaiton

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 April 2006 - 06:37 PM

This snippet should illustrate the use of checkbox arrays. Note that only the values of checked checkboxes are posted.

<?php
if (isset($_POST['submit'])) {

    foreach ($_POST['color'] as $color) {
             echo "$color was selected <br />";
    }
    echo '<hr />';
}
?>
<FORM method='post'>
<INPUT TYPE='CHECKBOX'  name='color[]' value='red'> Red<br/>
<INPUT TYPE='CHECKBOX'  name='color[]' value='green'> Green<br/>
<INPUT TYPE='CHECKBOX'  name='color[]' value='blue'> Blue<br/>
<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 07:25 PM

I ran into another problem, I cut off the globals, I rewrite my entire script, first I tried setting the $_POST['username'];
I tried setting those straight into the subject it didn't work so I tried registering all the variables like this.
<?php
$to = "businessman332211@hotmail.com";
$subject = "freelance businessman!";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$location = $_POST['location'];
$favorites = $_POST['favorites'];
$location = $_POST['location'];
$username = $_POST['username'];
$verifyusername = $_POST['verifyusername'];
$password = $_POST['password'];
$verifypassword = $_POST['verifypassword'];
$body = "
First Name: $firstname
Last Name: $lastname
Age: $age
Location: $location
Favorites: $favorites
Username: $username
Verify Username: $verifyusername
Password: $password
Verify Password: $verifypassword";
mail($to, $subject, $body);

if (mail($to, $subject, $body)) {
print "The email was sent successfully";
} else {
print "The email did not send successfully";
 }
?>
What is happening is it is sending an email properly, but nothing is coming up where the variables are supposed to be, what could be wrong, I registered the variables to specific names, and called hte variables where I needed them at, and it didn't work.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 08:03 PM

I have still been fighting with it trying different ways, trying ways I see in tutorials, but still can't get it to work.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 April 2006 - 08:13 PM

Do the keys of the $_POST array match exactly with the names of your form fields (case sensitive)?

EG

$firstname = $_POST['firstname'];

Is there a corresponding

<input type='text' name='firstname'>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 April 2006 - 08:17 PM

At the start of your script that recieves the data from your form, put these debugging lines to dump the contents of the "$_POST" and $_GET arrays:
<?php
echo '<pre>$_POST Array: ' . print_r($_POST,true) . '</pre>';
echo '<pre>$_GET Array: ' . print_r($_GET,true) . '</pre>';
?>

Ken

#10 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 08:27 PM

I actually I decided to run a validation on the form, I had action set twice, typo, and had checked=check instead of checked=checked, so it works now, now I am starting to play with setting those checked fields into an array and passing it to the message.

also, I would debug but I figured out the problem. with that

ok here is what I tried, with various other attempts but it doesn't seem to work.
<?php
foreach ($_POST['choices'] as $choices)
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$location = $_POST['location'];
$favorites = $_POST['favorites'];
$username = $_POST['username'];
$verifyusername = $_POST['verifyusername'];
$password = $_POST['password'];
$verifypassword = $_POST['verifypassword'];
$to = "businessman332211@hotmail.com";
$subject = "freelance businessman!";
$body = "
First Name: $firstname
Last Name: $lastname
Age: $age
Choices: $choices
Location: $location
Favorites: $favorites
Username: $username
Verify Username: $verifyusername
Password: $password
Verify Password: $verifypassword
";
mail($to, $subject, $body);

if (mail($to, $subject, $body)) {
print "The email was sent successfully";
} else {
print "The email did not send successfully";
 }
?>

heres the form code
<form name="testform" action="formprocessor.php" method="post">
First Name:<input name="firstname" type="text" maxlength="20" /><br />
Last Name:<input name="lastname" type="text" maxlength="20" /><br />
Age:<input name="age" type="text" maxlength="3" /><br />
How you located this Website:<br />
<select name="location">
<option>Search Engine</option>
<option>Browsing other</option>
<option>freelance businessman</option>
<option>You don't want to specify</option>
</select>
<br />
Pick one of the following:<br />
Video Games:<input name="favorites" type="radio" value="video games" checked="checked"/><br />
Tv:<input name="favorites" type="radio" value="tv" /><br />
Nothing:<input name="favorites" type="radio" value="nothing" /><br />
Please check all that apply:<br />
I am comfortable with my surroundings:<input name="choices[]" type="checkbox" value="I am comfortable with my surroundings" /><br />
I am angered by my surroundings:<input name="choices[]" type="checkbox" value="I am angered by my surroundings" />
<br />
I am bothered by my surroundings:<input name="choices[]" type="checkbox" value="I am bothered by my surroundings" />
<br />
I am worthless when people are around:<input name="choices[]" type="checkbox" value="I am worthless when people are around" />
<br />
I am unserious when people are around:<input name="choices[]" type="checkbox" value="I am unserious when people are around" />
<br />
Please choose a proper username:<input name="username" type="text" maxlength="30" /><br />
Please verify your username:<input name="verifyusername" type="text" maxlength="30" /><br />
Please add in your password:<input name="password" type="password" maxlength="25" /><br />
Please verify your password:<input name="verifypassword" type="password" maxlength="25" /><br />
<input name="submit" type="submit" value="submit" />
<input name="reset" type="reset" value="reset" />
Thank you for taking the time to fill out this form, if everything works correctly you will recieve a confirmation stating that it was completed successfully.

</form>
what I am trying to figure out is how to get that checkbox fields into an array and put them into an email that I have there to send to me, then after I get the basics I will know how I also tried setting the array manually on that page like
array($whatever, $whatever, )
and setting it like that, i also tried setting it manually with the keys as well and then passing it into the text, but nothing has worked so far.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 April 2006 - 08:43 PM

Your "foreach" statement is in the wrong place:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$location = $_POST['location'];
$favorites = $_POST['favorites'];
$username = $_POST['username'];
$verifyusername = $_POST['verifyusername'];
$password = $_POST['password'];
$verifypassword = $_POST['verifypassword'];
$to = "businessman332211@hotmail.com";
$subject = "freelance businessman!";
$body = "
First Name: $firstname
Last Name: $lastname
Age: $age\n";
foreach ($_POST['choices'] as $choices)
     $body .= "Choices: $choices\n";
$body .= "Location: $location
Favorites: $favorites
Username: $username
Verify Username: $verifyusername
Password: $password
Verify Password: $verifypassword
";
?>

Ken

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 April 2006 - 08:46 PM

$choices = join ("\r\n", $_POST['choices']);

and include $choices in the message.

to check contents
echo '<pre>';
echo $choices;
echo '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 April 2006 - 08:59 PM

wait answer htis question please, I really got confused when that worked. I saw about the /n when I was studying but was told it was for command line I figured I would never need it so I used <br /> instead this whole time, I tried your way, then with <br /> and your way worked, what happened please explain this too me, that code Iput it in like you said, and it came back, it had the values of what I had assigned on there, the only other thing I don't get is why it didn't print out the full message on there, but either way atleast it returned it to me, please explain what that /n is I know it's line feed but where does its usage come in at.

EDIT-one more question
also what was the $body that you put int here, I tried modifying it again and taking those out, can someone break this section down for me to help me understand, it would be great for me since I am about to move into more advanced stuff/.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users