# Help with php code



## BroBQ (May 1, 2013)

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!


```
<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>
```


----------



## BiggieShady (May 1, 2013)

Use 


```
isset($_POST['submit'])
```

to check if the form was posted

Use


```
$_POST['name']
$_POST['phone']
```

to get your posted values

and this is how you send the mail


```
$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 (May 1, 2013)

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:


```
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.


```
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 (May 1, 2013)

Aquinus said:


> 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?



That's nice rule based form validation  OP get inspired!


----------



## digibucc (May 1, 2013)

really aqui that's some damn good code. I may have to consider you a source of reference if you don't mind


----------



## Aquinus (May 1, 2013)

digibucc said:


> really aqui that's some damn good code. I may have to consider you a source of reference if you don't mind



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.


----------



## BroBQ (May 1, 2013)

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!


----------



## Aquinus (May 2, 2013)

Morrison5891 said:


> 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 ...



Explain what you mean by popup.

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


```
<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.


----------



## BroBQ (May 2, 2013)

Aquinus said:


> Explain what you mean by popup.
> 
> Do you mean a new window that loads some new content? Something like doing:
> 
> ...



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?


----------



## BroBQ (May 2, 2013)

Here is a good example of what i'm trying to do (sorta) only difference between the example and what I want is that... I want the window to only open if the alien ship is clicked.

http://www.dynamicdrive.com/dynamicindex5/stickytooltip.htm


----------



## Aquinus (May 2, 2013)

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.


----------



## n0tiert (May 2, 2013)

take a look on jquery form validation , easy use

http://designmodo.com/jquery-form-validation/


----------



## Castiel (May 6, 2013)

n0tiert said:


> take a look on jquery form validation , easy use
> 
> http://designmodo.com/jquery-form-validation/



I second this on the use of Jquery. Very easy.


----------



## BiggieShady (May 7, 2013)

Castiel said:


> I second this on the use of Jquery. Very easy.



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()


----------



## Aquinus (May 7, 2013)

BiggieShady said:


> 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()



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:

```
{
    "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.


----------



## W1zzard (May 7, 2013)

BiggieShady said:


> 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()



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 (May 22, 2013)

*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) .


----------



## W1zzard (May 22, 2013)

put a hidden input field

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


----------



## Aquinus (May 22, 2013)

W1zzard said:


> put a hidden input field
> 
> <input type="hidden" name="id" value="insert your id here" />



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.


----------



## n0tiert (May 22, 2013)

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; ?>" />


----------



## Easy Rhino (May 23, 2013)

Aquinus said:


> 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.
> ...



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.


----------



## Aquinus (May 23, 2013)

Easy Rhino said:


> 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.



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


----------



## W1zzard (May 23, 2013)

Aquinus said:


> client side validation



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


----------



## Aquinus (May 23, 2013)

W1zzard said:


> 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



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.


----------



## W1zzard (May 23, 2013)

Aquinus said:


> 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.



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 (May 23, 2013)

W1zzard said:


> just wanted to throw the library mentions in there, before people try building something out of html 5 validation



Very true. JQuery will work on more browsers than HTML5 will, that's for sure.


----------



## BiggieShady (May 23, 2013)

Easy Rhino said:


> 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.





Aquinus said:


> 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.



Example for what Aquinus said are username and email fields on the registration form. 

On client side you should check if username is long enough, has only allowed characters and for email if it's right format.
On server side you should check if username or email already exist in the database users table.
Some user registration forms may do all validation on server side, but none can do all validation on client side.


----------



## W1zzard (May 23, 2013)

BiggieShady said:


> On client side you should check if username is long enough, has only allowed characters and for email if it's right format.
> On server side you should check if username or email already exist in the database users table.



and also check on server side that it's long enough, has allowed characters etc. javascript is very easy to bypass


----------

