Jump to content

specific questions


Ninjakreborn

Recommended Posts

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
[code]
<?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>");
}
?>
[/code]
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.
[code]
<?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 />";
?>
[/code]
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.
Link to comment
Share on other sites

[!--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).
Link to comment
Share on other sites

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:
[code]
<?

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

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

[code]<?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>[/code]
Link to comment
Share on other sites

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.
[code]
<?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";
}
?>
[/code]
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.
Link to comment
Share on other sites

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:
[code]<?php
echo '<pre>$_POST Array: ' . print_r($_POST,true) . '</pre>';
echo '<pre>$_GET Array: ' . print_r($_GET,true) . '</pre>';
?>[/code]

Ken
Link to comment
Share on other sites

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.
[code]
<?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";
}
?>
[/code]

heres the form code
[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>
[/code]
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.
Link to comment
Share on other sites

Your "foreach" statement is in the wrong place:
[code]<?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
";
?>[/code]

Ken
Link to comment
Share on other sites

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.

[b]EDIT-one more question[/b]
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/.
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.