1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Help with php code

Discussion in 'Programming & Webmastering' started by Morrison5891, May 1, 2013.

  1. Morrison5891

    Morrison5891 New Member

    Joined:
    Mar 13, 2009
    Messages:
    806 (0.39/day)
    Thanks Received:
    91
    Location:
    Bannock County
    I have been trying to get the information in this contact form to be required by the user. Right now users can click on the "contact us" button and it forwards them to the welcome page. What happens is when someone clicks the button it sends an e-mail with the values "Enter Your First Name" and "Enter Your Phone No." It does send the correct information if you actually enter your name and phone number. This form is located at: http://www.mycompumore.com/landing/service/

    Any help would be greatly appreciated!

    Code:
    <form action="contact.php" method="post">
    				<input  name="name" id="name" type="text" class="name_field"  onFocus="if(this.value == 'Enter Your First Name'){ this.value = ''; this.style.color= '#1a1a1a'; }" onBlur="if(this.value == ''){ this.value = 'Enter Your First Name'; this.style.color= '#818181'; }" value="Enter Your Name" required />
    				<br />
                    
    				<input type="text"  name="phone" id="phone" class="phone_field" value="Enter Your Phone No." onFocus="if(this.value == 'Enter Your Phone No.'){ this.value = ''; this.style.color= '#1a1a1a'; }" onBlur="if(this.value == ''){ this.value = 'Enter Your Phone No.'; this.style.color= '#818181'; }" />
    				<br />
    
    				<input type="submit" name="submit" value="" class="opt_btn" />
    			
    </form>    
     
  2. BiggieShady

    BiggieShady

    Joined:
    Feb 8, 2012
    Messages:
    1,025 (1.00/day)
    Thanks Received:
    352
    Location:
    Zagreb, Croatia
    Use

    Code:
    isset($_POST['submit']) 
    to check if the form was posted

    Use

    Code:
    $_POST['name']
    $_POST['phone']
    to get your posted values

    and this is how you send the mail

    Code:
    $to      = 'someone@there.com';
    $subject = 'Test';
    $message = 'Test message';
    $headers = 'From: me@here.com' . "\r\n" .
        'Reply-To: me@here.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    
    This will work on remote server, but won't work on local server unless mail server is installed and SMTP is correctly configured. Alternatively you can use a PHP classs that supports SMTP authentication, such as Zend_Mail or PHPMailer.
     
    Aquinus says thanks.
  3. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    Just to add a little bit to what has already been added:

    The API for the PHP call to mail() can be found here: http://php.net/manual/en/function.mail.php
    I'm willing to bet you want to know how it works, not just how to use it. :)

    As for checking the form, you can do what was prescribed but it sounds like you want to do some checking of the data to make sure that it is correct or that both fields have content. I recommend writing either a small function or a class to handle form interactions.

    So to get the data from the form, I would do something like this if I didn't write a class:

    Code:
    function get_submitted_form($fields) {
        return array_reduce($fields, function(&$r, $d) {
            if (!empty($_POST[$d])) { $r[$d] = $_POST[$d]; }
            return $r
        }, array());
    }
    Then you call it with an array of the form fields.

    Code:
    get_submitted_form(array('name', 'phone', 'submit'));
    This is awfully simplified and requires PHP 5.3. If you need to use forms elsewhere I would consider a more well thought out solution like a class that manages forms in general (not just data retrieval, but data validation).

    Not that this helps, but a web framework I was working on several months ago and I do have a form library, but it's far from perfect and requires other modules within the framework. Maybe it will inspire you? Feel free to use it but remember it's GPL v3 so make sure if you use it to preserve copyright and ensure the license you're using is GPL compatible.
    https://github.com/jrdoane/PlumPHP/blob/portal/ext/form.php
     
    BiggieShady says thanks.
  4. BiggieShady

    BiggieShady

    Joined:
    Feb 8, 2012
    Messages:
    1,025 (1.00/day)
    Thanks Received:
    352
    Location:
    Zagreb, Croatia
    That's nice rule based form validation :toast: OP get inspired! :cool:
     
  5. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,937 (2.45/day)
    Thanks Received:
    1,502
    really aqui that's some damn good code. I may have to consider you a source of reference if you don't mind ;)
     
  6. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    Feel free, it's all GPL 3.0 so have at it. Most of the code I write and publish is open source, so that's what it is there for. Sharing is caring. :)

    Also as a side note, the code might be slightly out of date. I have it running in production as a login portal at work and it has done a pretty good job. If I have some time later today I'll make sure that I've back-ported any changes that I've made since the last time I committed.

    I haven't worked on it too as of late. Between projects at work and getting better at Ruby and Clojure I haven't had a whole lot of time. In fact I'm trying to write PHP less because of some of it's absurdity that drives me nut and honestly, I've really been liking Ruby.
     
    cadaveca, digibucc and Mindweaver say thanks.
  7. Morrison5891

    Morrison5891 New Member

    Joined:
    Mar 13, 2009
    Messages:
    806 (0.39/day)
    Thanks Received:
    91
    Location:
    Bannock County
    Appreciate all the feedback. I will get this fixed ASAP!

    Now, if you wouldn't mind, I have another question. I'm not really sure how to ask this, as I don't know know much about what i'm trying to do. :) Anyway, Now... if you visit another section of our site... http://www.mycompumore.com you will see a flying "alien ship". it uses jQuery, jQuery spritely, html, and CSS

    What I would like to do, if the ship is clicked on have a pop up window "pop up" with some text in the window for information, etc ...

    Is this possible?

    Thanks again!
     
  8. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    Explain what you mean by popup.

    Do you mean a new window that loads some new content? Something like doing:

    Code:
    <a href="http://somewebsite.com/" target="_blank">Content here.</a>
    http://www.w3schools.com/tags/att_a_target.asp

    Or an alert generated by JavaScript using alert()?

    Or maybe this?
    http://www.w3schools.com/jsref/met_win_createpopup.asp
    I wouldn't recommend that though as it's only supported in IE. (Gah, MS.)

    I would be careful with popups though, most browsers block them by default. It's not good practice to use them IMHO.
     
  9. Morrison5891

    Morrison5891 New Member

    Joined:
    Mar 13, 2009
    Messages:
    806 (0.39/day)
    Thanks Received:
    91
    Location:
    Bannock County
    For example, if a user clicks on the "alien ship" a window pops up with a coupon for service. would there be a way to the alien ship clickable?
     
  10. Morrison5891

    Morrison5891 New Member

    Joined:
    Mar 13, 2009
    Messages:
    806 (0.39/day)
    Thanks Received:
    91
    Location:
    Bannock County
  11. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    Okay, I see what you mean.

    It might sound cool to do, but from a UI perspective it's not clear until you hover over or click the ship for you to get the page. It might sound neat to do, but people will miss it. Keep in mind that the first thing you want to consider when building any web application is ease of use. I would just add content to the page straight up with what you want to say, or put it in the context that you want it to appear, such as service work for new accounts that get opened. Stuff like that.

    Now, I'm confused by what you mean coupon service. Do you mean a site hosted by another company or do you mean coupons for the user to get money off service to their computer? If it's the latter you want to present the user that information when it's relevant such as when you're making a request for service to be done, or maybe right before that page. That way you're targeting the users who will be getting the service as opposed to anyone who visits the website.

    How a person interacts with an application is a big factor in the success and usability of any piece of software and you should almost always assume that the user is dumb as a door nail when designing the UI if it's something for the general public.

    With that said, I'm still not sure what you want; A new window or a tooltip-like hovering box rendered on the page?

    Edit: I'm checking for anything that needs to be backported to PlumPHP right now.

    Edit 2: I think that master has all the latest changes. I think that the portal branch is out of date. I wouldn't recommend using that anyways, it's a better example of how it can be used more than anything else, but if you want the form extension, you'll need to grab it from that branch first.
     
    Last edited: May 2, 2013
    digibucc says thanks.
  12. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    925 (0.38/day)
    Thanks Received:
    404
    Location:
    Frankfurt/Main - Germany
  13. Castiel

    Castiel

    Joined:
    May 5, 2008
    Messages:
    3,319 (1.38/day)
    Thanks Received:
    310
  14. BiggieShady

    BiggieShady

    Joined:
    Feb 8, 2012
    Messages:
    1,025 (1.00/day)
    Thanks Received:
    352
    Location:
    Zagreb, Croatia
    Jquery is fine for things like input format validation but often you have to validate on server side against the database. In that case you would use php and jquery's $.ajax()
     
  15. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    This, but make sure to wrap the AJAX response in JSON so you have a structure to the data you're collecting. If you ever end up doing more stuff with JQuery and AJAX, you'll want something standardized that you can work off of it for different AJAX calls in the future.

    Something simple would be like this:
    Code:
    {
        "success" : 1,
        "result" : {
            "data1" => "some data",
            "data2" => "more data"
        }
    }
    You could (in development) include some debug information such as execution time and such as well.

    That way you can always rely on every ajax call have certain data, but still have unique data for each call. This really starts getting into web API design though. This is another thing I have some experience with as I've worked on building a REST server and I wrote a minimal SAML 2.0 IDP and several SPs from scratch with the help of the php xmlseclibs library and xmlsec1.
     
    owen says thanks.
  16. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,013 (3.90/day)
    Thanks Received:
    11,879
    Yes, always use server-side validation, too.

    But when I implement something with jquery form validation, I keep the server side code simple, on error just die() with the error message, no more fancy code needed to return people to the form with their input data saved.
     
    owen and digibucc say thanks.
  17. owen New Member

    Joined:
    May 20, 2013
    Messages:
    1 (0.00/day)
    Thanks Received:
    0
    PHP Script Help

    Hi,

    I was just testing an ADD/DELETE/EDIT script in PHP but i cant get the EDIT to work properly. I know what the problem is but im not sure how to fix it.

    When EDIT is clicked on one of the names it uses the "WHERE ID=$ID" to get the information about that name. The problem is when SUBMIT is clicked, the ID is not passed along to the part of the script that edits the information in the database, so it doesnt know which record to edit.

    All i need is a way of passing the ID through the form to the next part of the page so that it would work.

    At the moment I have to put the ID Field in and it works (but i know its not a very good way of doing it) .
     
  18. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,013 (3.90/day)
    Thanks Received:
    11,879
    put a hidden input field

    <input type="hidden" name="id" value="insert your id here" />
     
  19. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    This, or make the form action include the param as a get variable in the url but using hidden form elements is more clean imho.
     
  20. n0tiert

    n0tiert

    Joined:
    May 1, 2008
    Messages:
    925 (0.38/day)
    Thanks Received:
    404
    Location:
    Frankfurt/Main - Germany
    if you collect the $ID earlier due edit function you should pass the retrieved $ID like this back to the server,
    a online place to test php code:

    http://writecodeonline.com/php/

    Add this to Inputfield

    $ID = "1234";
    ?>
    <input type="text" name="id" value="<?php echo $ID; ?>" />
     
    Last edited: May 22, 2013
  21. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,506 (4.60/day)
    Thanks Received:
    3,357
    not to be a dick here but html5 already does a lot of your more basic form validation. you don't need to spend time coding something up to cover for empty fields or numbers where letters should be. just fyi to the OP.
     
    Crunching for Team TPU
  22. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    Not to be a dick back but HTML5 only offers client side validation. I would hope that you would make sure that the data entering the application is valid anyways regardless. Client side validation is more for the client's sake than for the server's. It makes more sense to stop a user prior to POSTing the form when validation fails and telling the user as opposed to only carping if data wasn't passed forward properly by the time you get to PHP, but regardless of client side validation you want to make sure data is correct by the time it enters the server.

    Doing it this way you can easily tell if a form element was never used just by checking if the object element is "isset" or not. I like to make sure that I have everything that I need before proceeding. Also by processing the form fields that you defined the form itself with, you'll be using data you already have to enable you to put the form in the format you'll already want it in. Redefining the structure of the data that you're going to get is repetitive and counterproductive.

    Obviously this isn't an ideal scenario but you don't want to define how data is supposed to look in more than one place. That's me though.

    I would opt for form library that behaves like this: http://doc.nette.org/en/forms
     
    Last edited: May 23, 2013
    Easy Rhino says thanks.
  23. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,013 (3.90/day)
    Thanks Received:
    11,879
    I like to use jQuery Validate for that (even in HTML5), it has a learning curve, and the documentation sucks. Using it combined with something like jQuery noty to show errors. See it in action in the case gallery, add new case
     
  24. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,688 (6.46/day)
    Thanks Received:
    2,341
    Location:
    Concord, NH
    I get that. I'm not disputing that. When someone posts something to the forum I bet you the server checks it before it gets processed and put into the database, correct? That's the point I'm trying to make. Regardless of whatever you end up using, you still want to check it on the server when everything is said and done.
     
  25. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,013 (3.90/day)
    Thanks Received:
    11,879
    of course, never trust any user input.

    just wanted to throw the library mentions in there, before people try building something out of html 5 validation :)
     
    Aquinus says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page