Jump to content

php_discipulus

Members
  • Posts

    3
  • Joined

  • Last visited

php_discipulus's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. On page 111 from the book PHP Solutions Dynamic Web design made easy Second Edition. There was a code that made sure the fields aren't blank can somebody explain this code to me please I am having a hard time understanding it foreach($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { $missing[] = $key; } elseif (in_array($key, $expected)) { // otherwise, assign to a variable of the same name as $key ${$key} = $temp; } } and here is the rest of the code for the form: <?php $missing = array(); if (isset($_POST['send'])) { $to = 'myemail@example.com'; $subject = 'Form subject'; $expected = array('name', 'email', 'comment'); $required = array('name', 'email', 'comment'); } ?> <form method="post" action=""> <p> <label for="name">Enter your name:</label><br> <input type="text" name="name" id="name" /> </p> <p> <label for="email">Enter your email:</label><br> <input type="text" name="email" id="email" /> </p> <p> <label for="email">Enter comment:</label><br> <textarea name="comment" id="comment" cols="40" rows="10"></textarea> </p> <p> <input type="submit" name="send" id="send" value="Send" /> </p> </form>
  2. I just made this form in php (nowhere near finished) that checks for user input in the form, if there is no input it gives an error, if there is input and the user submits a form no previous entered data is lost, So what you think is this correct? Can I improve it in anyway? <?php $missing = array(); if (isset($_POST['send'])) { $to = 'myemail@example.com'; $subject = 'Form subject'; $expected = array('name', 'email', 'comment'); $required = array('name', 'email', 'comment'); foreach($_POST as $key => $value) { // Assign to temp var to value $temp = (is_array($value)) ? $value : trim($value); // Store empty values in missing array if (empty($temp) && in_array($key, $required)) { $missing[] = $key; } elseif (empty($temp) && in_array($key, $expected)) { ${$key} = $temp; } } // If you find something missing // keep the same value of user input $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; } ?> <form method="post" action=""> <p> <label for="name">Enter your name:</label><br> <input type="text" name="name" id="name" value="<?php if(!empty($value) && !(in_array('name', $missing))) { echo $name; } ?>" /> <?php if (in_array('name', $missing)) { echo "You missed out your name"; } ?> </p> <p> <label for="email">Enter your email:</label><br> <input type="text" name="email" id="email" value="<?php if(!empty($value) && !(in_array('email', $missing))) { echo $email; } ?>" /> <?php if (in_array('email', $missing)) { echo "You missed out your email"; } ?> </p> <p> <label for="email">Enter comment:</label><br> <textarea name="comment" id="comment" cols="40" rows="10"><?php if(!empty($value) && !(in_array('comment', $missing))) { echo $comment; } ?></textarea> <?php if (in_array('comment', $missing)) { echo "You missed out your comment"; } ?> </p> <p> <input type="submit" name="send" id="send" value="Send" /> </p> </form>
  3. Hi, New to php here please help me out. So I have been trying to use the mail function to send mail to me, but it's not working I know i changed the fakeemail@example.com to mine, but it's not working and I can't figure out why. Plus do you know how I can add the emailers name and his email sent additionally with the message to me. Thanks. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' ) { if (mail('fakeemail@example.com','New Website Message', $_POST['message'])) { $status = "Thank you for your message {$_POST['email']}"; } } ?> <html> <head> <title></title> <style> label {display: block;} form ul {margin: 0; padding: 0;} form li {list-style: none; margin-bottom: 20px;} </style> </head> <body> <h1>Contact Form</h1> <form action="" method="post"> <ul> <li> <label for="name">Name: </label> <input type="text" name="name" id="name"> </li> <li> <label for="email">Email: </label> <input type="text" name="email" id="email"> </li> <li> <label for="message">Your Message: </label><br /> <textarea name="message" id="message" cols="30" rows="10"></textarea> </li> <li> <input type="submit" value="Go!"> </li> </ul> </form> <?php if (isset($status)) echo $status; ?> </body> </html>
  4. Joined the forum, and looking forward to learning together with you guys and girls. Currently, I am just watching tutorials and video courses on php and after I have a good grasp of the basics of php and mysql I will start a small project and build from there. So what do you think about this approach? The tutorial I am watching is php fundamentals by Jeffrey Way. Nice tutorial, but it's hard to remember everything. Anyway let me know what was the most effective way of learning for you when you started learning php and mysql. See you later guys and girls
×
×
  • 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.