• We've upgraded our forums. Please post any issues/requests in this thread.

Help with php code

Joined
Mar 13, 2009
Messages
816 (0.26/day)
Likes
93
Location
Bannock County
Processor AMD FX-8320 4.1Ghz
Motherboard Asus Aura
Cooling Corsair H60
Memory Crucial Ballistix Sport 32GB
Video Card(s) EVGA 1060 SC
Storage Intel 535 Series SSD + Seagate FireCuda 2TB
Case NZXT
Power Supply EVGA 650 Watt
Software Windows 10 Pro 64-Bit
#1
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>
 
Joined
Feb 8, 2012
Messages
2,678 (1.26/day)
Likes
1,914
Location
Zagreb, Croatia
System Name Windows 7 64-bit Core i5 3570K
Processor Intel Core i5 3570K @ 4.2 GHz, 1.26 V
Motherboard Gigabyte GA-Z77MX-D3H
Cooling Scythe Katana 4
Memory 4 x 4 GB G-Skill Sniper DDR3 @ 1600 MHz
Video Card(s) Gainward NVIDIA GeForce GTX 970 Phantom
Storage Western Digital Caviar Blue 1 TB, Seagate Baracuda 1 TB
Display(s) Dell P2414H
Case CoolerMaster Silencio 550
Audio Device(s) VIA HD Audio
Power Supply Corsair TX v2 650W
Mouse Steelseries Sensei
Keyboard CM Storm Quickfire Pro, Cherry MX Reds
Software MS Windows 7 Enterprise 64-bit SP1
#2
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#3
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
 
Joined
Feb 8, 2012
Messages
2,678 (1.26/day)
Likes
1,914
Location
Zagreb, Croatia
System Name Windows 7 64-bit Core i5 3570K
Processor Intel Core i5 3570K @ 4.2 GHz, 1.26 V
Motherboard Gigabyte GA-Z77MX-D3H
Cooling Scythe Katana 4
Memory 4 x 4 GB G-Skill Sniper DDR3 @ 1600 MHz
Video Card(s) Gainward NVIDIA GeForce GTX 970 Phantom
Storage Western Digital Caviar Blue 1 TB, Seagate Baracuda 1 TB
Display(s) Dell P2414H
Case CoolerMaster Silencio 550
Audio Device(s) VIA HD Audio
Power Supply Corsair TX v2 650W
Mouse Steelseries Sensei
Keyboard CM Storm Quickfire Pro, Cherry MX Reds
Software MS Windows 7 Enterprise 64-bit SP1
#4
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 :toast: OP get inspired! :cool:
 
Joined
May 21, 2009
Messages
4,966 (1.59/day)
Likes
1,511
System Name i7-PC / HTPC / iMac
Processor i7 3820 / Phenom II 940
Motherboard GIGABYTE G1.ASSASSIN2 / M3A79-T Deluxe
Cooling Corsair Hydro H100i / Scythe II (HS only)
Memory G.SKILL Trident X Series 8GB (2 x 4GB) DDR3 1600mhz / 4GB DDR2 1066 (@800) Corsair Dominator
Video Card(s) GB Radeon HD 7950s 3GB / GB Radeon HD 7950s 3GB
Storage 2x 80GB Intel X-25, 2x600gb SATA, 1x1tb 5400RPM storage /1x600GB, 3x500GB,1x160,1x120 SATA
Display(s) 1x 27" Yamakasi / Vizio 42" HDTV
Case Lian Li Lancool PC-K58 / Antec 900
Audio Device(s) HT Omega Striker 7.1 / Onboard and HDMI from ATi Card
Power Supply PC Power & Cooling 750W / 610W
Software Ubuntu / Windows 8.1 Pro / OS X / PHPStorm / Gaming
#5
really aqui that's some damn good code. I may have to consider you a source of reference if you don't mind ;)
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#6
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.
 
Joined
Mar 13, 2009
Messages
816 (0.26/day)
Likes
93
Location
Bannock County
Processor AMD FX-8320 4.1Ghz
Motherboard Asus Aura
Cooling Corsair H60
Memory Crucial Ballistix Sport 32GB
Video Card(s) EVGA 1060 SC
Storage Intel 535 Series SSD + Seagate FireCuda 2TB
Case NZXT
Power Supply EVGA 650 Watt
Software Windows 10 Pro 64-Bit
#7
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#8
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:

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.
 
Joined
Mar 13, 2009
Messages
816 (0.26/day)
Likes
93
Location
Bannock County
Processor AMD FX-8320 4.1Ghz
Motherboard Asus Aura
Cooling Corsair H60
Memory Crucial Ballistix Sport 32GB
Video Card(s) EVGA 1060 SC
Storage Intel 535 Series SSD + Seagate FireCuda 2TB
Case NZXT
Power Supply EVGA 650 Watt
Software Windows 10 Pro 64-Bit
#9
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.)
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?
 
Joined
Mar 13, 2009
Messages
816 (0.26/day)
Likes
93
Location
Bannock County
Processor AMD FX-8320 4.1Ghz
Motherboard Asus Aura
Cooling Corsair H60
Memory Crucial Ballistix Sport 32GB
Video Card(s) EVGA 1060 SC
Storage Intel 535 Series SSD + Seagate FireCuda 2TB
Case NZXT
Power Supply EVGA 650 Watt
Software Windows 10 Pro 64-Bit
#10

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#11
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:
Joined
May 1, 2008
Messages
1,038 (0.30/day)
Likes
564
Location
Frankfurt/Main - Germany
System Name Shaman of Sexy
Processor AMD Phenom II X4 955 BE@4Ghz EK Supreme Block
Motherboard M3A79-T Deluxe Anfi-Tech Waterblocks
Cooling Magicool 360 + 120 + 120 Slim scythe slipped Laing DDC-1/T
Memory 4GB Corsair Dominator CM2X2048-8500C5D
Video Card(s) Sapphire ATI Radeon HD 4870 X2 EK 4870 X2 Block
Storage RAID 0 Seagate
Display(s) Samsung 226BW 22"
Case CoolerMaster Cosmos RC-1000 in mod progress
Audio Device(s) onboard
Power Supply Coba Nitrox 750W
Software Windows 7 Ultimate
Benchmark Scores http://service.futuremark.com/compare?3dmv=1056967
#12
Joined
May 5, 2008
Messages
3,318 (0.95/day)
Likes
301
Processor Intel i5-3570K @ 3.4Ghz
Motherboard Asrock LGA1155 Z77 Extreme 4
Cooling Cooler Master Evo 212
Memory 16GB (4X4) G.Skill Ripjaw 2 DDR3-1600
Video Card(s) Nvidia gForce GTX 660ti
Storage 1x Samsung 840 EVO 256GB 6Gb/s, 1x WD 500GB 6Gb/s, 1x WD 80GB 3Gb/s
Display(s) ASUS VH242H Black + 2 HP 2311x 23" LED
Case Fractal Design R4
Audio Device(s) Realtek OnBoard Both
Power Supply Cooler Master 850w
Software Windows 7 Ultimate 64-bit SP1
#13
Joined
Feb 8, 2012
Messages
2,678 (1.26/day)
Likes
1,914
Location
Zagreb, Croatia
System Name Windows 7 64-bit Core i5 3570K
Processor Intel Core i5 3570K @ 4.2 GHz, 1.26 V
Motherboard Gigabyte GA-Z77MX-D3H
Cooling Scythe Katana 4
Memory 4 x 4 GB G-Skill Sniper DDR3 @ 1600 MHz
Video Card(s) Gainward NVIDIA GeForce GTX 970 Phantom
Storage Western Digital Caviar Blue 1 TB, Seagate Baracuda 1 TB
Display(s) Dell P2414H
Case CoolerMaster Silencio 550
Audio Device(s) VIA HD Audio
Power Supply Corsair TX v2 650W
Mouse Steelseries Sensei
Keyboard CM Storm Quickfire Pro, Cherry MX Reds
Software MS Windows 7 Enterprise 64-bit SP1
#14
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#15
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:
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.
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
17,026 (3.43/day)
Likes
17,894
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#16
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

New Member
Joined
May 20, 2013
Messages
1 (0.00/day)
Likes
0
#17
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

Administrator
Staff member
Joined
May 14, 2004
Messages
17,026 (3.43/day)
Likes
17,894
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#18
put a hidden input field

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

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#19
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.
 
Joined
May 1, 2008
Messages
1,038 (0.30/day)
Likes
564
Location
Frankfurt/Main - Germany
System Name Shaman of Sexy
Processor AMD Phenom II X4 955 BE@4Ghz EK Supreme Block
Motherboard M3A79-T Deluxe Anfi-Tech Waterblocks
Cooling Magicool 360 + 120 + 120 Slim scythe slipped Laing DDC-1/T
Memory 4GB Corsair Dominator CM2X2048-8500C5D
Video Card(s) Sapphire ATI Radeon HD 4870 X2 EK 4870 X2 Block
Storage RAID 0 Seagate
Display(s) Samsung 226BW 22"
Case CoolerMaster Cosmos RC-1000 in mod progress
Audio Device(s) onboard
Power Supply Coba Nitrox 750W
Software Windows 7 Ultimate
Benchmark Scores http://service.futuremark.com/compare?3dmv=1056967
#20
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:

Easy Rhino

Linux Advocate
Joined
Nov 13, 2006
Messages
14,405 (3.56/day)
Likes
4,256
System Name VHOST01 | Desktop
Processor i7 980x | i5 7500 Kaby Lake
Motherboard Gigabyte x58 Extreme | AsRock MicroATX Z170M Exteme4
Cooling Prolimatech Megahelams | Stock
Memory 6x4 GB @ 1333 | 2x 8G Gskill Aegis DDR4 2400
Video Card(s) Nvidia GT 210 | Nvidia GTX 970 FTW+
Storage 4x2 TB Enterprise RAID5 |Corsair mForce nvme 250G
Display(s) N/A | Dell 27" 1440p 8bit GSYNC
Case Lian Li ATX Mid Tower | Corsair Carbide 400C
Audio Device(s) NA | On Board
Power Supply SeaSonic 500W Gold | Seasonic SSR-650GD Flagship Prime Series 650W Gold
Mouse N/A | Logitech G900 Chaos Spectrum
Keyboard N/A | Posiden Z RGB Cherry MX Brown
Software Centos 7 | Windows 10
#21
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'));
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#22
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
 
Last edited:

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
17,026 (3.43/day)
Likes
17,894
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#23
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,398 (4.85/day)
Likes
5,477
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#24
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

Administrator
Staff member
Joined
May 14, 2004
Messages
17,026 (3.43/day)
Likes
17,894
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#25
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 :)