...

View Full Version : Copy / Paste simple code for PHP form?



cineweekly.com
11-14-2011, 11:17 AM
Could someone please help me with a very simple form that I can just copy and paste that includes a drop down menu, a text area, and a series of checkboxes ("choose all that apply"). I would like for the results to be e-mailed to me upon clicking submit. I have a basic understanding of forms so bear with me.

myfayt
11-14-2011, 03:20 PM
Not enough information

cineweekly.com
11-14-2011, 10:32 PM
Not enough information

What else would you need? I have a similar working form and this description is pretty straight forward.

Rowsdower!
11-14-2011, 10:39 PM
You might get a better response if you show us what you have tried so far. Barring that, you might as well search Google for some form generators and just run with that.

You know by now that most people here are keen to help, but not so keen on providing spoon-fed code. :thumbsup:

cineweekly.com
11-15-2011, 12:37 AM
You might get a better response if you show us what you have tried so far. Barring that, you might as well search Google for some form generators and just run with that.

You know by now that most people here are keen to help, but not so keen on providing spoon-fed code. :thumbsup:

I tried previously to get help with my current code but everyone has different methods and it became a jumbled mess so I figured we'd go from scratch. The working form I do have is so incredibly simple and short that I wouldn't have a problem giving it away to someone that didn't know how to do it. I'll PM you my code and the single problem it's giving me if you want to help with it that way.

mvmacd
11-15-2011, 01:10 AM
Could someone please help me with a very simple form that I can just copy and paste that includes a drop down menu, a text area, and a series of checkboxes ("choose all that apply"). I would like for the results to be e-mailed to me upon clicking submit. I have a basic understanding of forms so bear with me.

Try this.

action.php:




<?php

if ( empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'] ) )
{
die("Some required fields were missing. Go back and try again");
}

$your_email = 'YOUR_MAIL_HERE@website.com';
$name = $_POST['name'];
$email = $_POST['email'];
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$message = $_POST['message'];


if ( mail($your_email, "Message from $name", "Name: $name\n
Email: $email\n
option1: $option1\n
option2: $option2\n
Message: \n
$message
", "From:" . $email)
)
{
echo "Your message was sent successfully!";
}
else
{
echo "Something went wrong. Plese try again.";
}
?>


mail.html:


<html>

<body>
<p>
<form action='action.php' method='post'>
Name<input type='text' name='name' /><br>
Email<input type='text' name='email' /><br>
Check all that apply:<br>
option1 <input name='option1' type='checkbox' /><br>
option2 <input name='option2' type='checkbox' /><br>
Message: <br>
<textarea name='message' rows='10' cols='40'>
</textarea><br>
<input type='submit' value='Submit' />
</p>
</body>

</html>

cineweekly.com
11-15-2011, 04:16 AM
That seems to be working but how would I do the checkbox portion as an array instead of multiple variables? For example, some sites have shown naming all checkboxes the same and adding [] to the end. I did that but in the e-mail it just sends the word "Array". I think I'm just missing some PHP (not too familiar with it).


<input name='options[]' type='checkbox' value='One' /><br>
<input name='options[]' type='checkbox' value='Two' />

In this example, if both boxes were checked, I would expect the e-mail to read "One Two".

Rowsdower!
11-15-2011, 02:55 PM
I just replied to your PM with basically the same thing. To fix the Array problem just implode the array into your e-mail message string with ", " as the "glue" parameter and things should get back on track.

cineweekly.com
11-15-2011, 10:37 PM
I just replied to your PM with basically the same thing. To fix the Array problem just implode the array into your e-mail message string with ", " as the "glue" parameter and things should get back on track.

That's where I'm getting stuck, I can usually figure out form issues but PHP I simply don't know. I've tried playing with the implode code in the link you sent me but I just don't know enough about it. I don't know when to replace $array with $features or $features[] and when to leave the word "array" alone, etc. Thanks for the help so far. Soooo close.

Rowsdower!
11-16-2011, 12:20 AM
I'll grant you, I didn't test the script edit that I PM'd you, but I'm pretty sure it should have worked right out of the box. If not, what errors are you getting?

cineweekly.com
11-16-2011, 03:01 AM
In the PM we changed the checkboxes names to features[] and when the form is submitted, I'm sent an e-mail with every other field being correct while the features checkboxes only says the word "Array" instead of the actual values that make up the array.

jazzerup
11-16-2011, 07:05 AM
Hi cineweekly.com,

Rather than coding from scratch I recommend you use google forms. You can set it up to automatically email you and save the results into a spreadsheet that you can export into excel. It has a really easy form building interface and best of all it's free =)

I have a tutorial here: http://www.jazzerup.com/blog/item/googleforms

Jaz

cineweekly.com
11-16-2011, 07:25 AM
It's free but branded all over by Google. Thanks anyway. I'm going for a more professional look and we're literally a couple of lines of code away from what I need.

EDIT: Hmmm, jazzerup, the more I look into your site the more I'm thinking you're here to sell stuff.

Open-Source
11-16-2011, 12:33 PM
Above all post are very well structured but here i would like to suggest you that please Concentrate on Validation, if your Form have no validation then its worthless. Kindly add validation on Form.
_________________________
Open Source Web Developmet (http://www.open-source-web-development.com/)

Rowsdower!
11-16-2011, 04:19 PM
In the PM we changed the checkboxes names to features[] and when the form is submitted, I'm sent an e-mail with every other field being correct while the features checkboxes only says the word "Array" instead of the actual values that make up the array.

The PM'd code also changed your line of code that populates the $message so that it implodes the array and separates the items with commas:


$message="Title: $title \nURL: $url \nE-mail: $email \nDescription: $description \nGenre: $genre \nFeatures:\n ".implode(", ",$features)." \nUpdate: $update\n Age: $age \nMovies: $movies \nReference: $reference \nOther: $other";

I believe that is the 15th line of code in your script.

Did you update that line in your script as well? If so then there is no reason that the "Array" text should be plopped into the e-mail message.

cineweekly.com
11-16-2011, 11:36 PM
I got so excited, I accidentally sent a PM instead of replying here.


Anyway, that did it. I missed that part due to horizontal scroll. In the end I just needed to add [] to the checkbox names and ".implode(", ",$features)." to the e-mail's message. I knew I was just a couple of lines of code off. Thanks again!

cineweekly.com
11-19-2011, 06:50 AM
I just noticed I'm getting an error_log for this page.
PHP Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in form.html on line 49

This is line 49:

$message="Title: $title \nURL: $url \nE-mail: $email \nDescription: $description
\nFeatures: ".implode(", ",$features)." \nGenre: $genre \nUpdate: $update \nAge:
$age \nMovies: $movies \nReference: $reference \nOther: $other";

cineweekly.com
11-25-2011, 06:50 PM
Bump.
I would really really appreciate help with the previous post. I'm getting a lot of traffic to this page and every page view sends me this error. Thanks so much.

Rowsdower!
11-28-2011, 03:00 PM
There isn't much to figure out with this one since there are only two parameters for the function. Either the text string "glue" parameter wasn't text or the array parameter passed was not actually an array. Since we can clearly see that the text string is a text string then that only leaves the array variable somehow not being an array.

So we need to check the array variable's type to see if it is an array before we try to implode it. How do you suppose you might go about doing that?

Check google and PHP.net and see if you can come up with some ideas. Post back here when you have something.

cineweekly.com
11-30-2011, 05:24 AM
There isn't much to figure out with this one since there are only two parameters for the function. Either the text string "glue" parameter wasn't text or the array parameter passed was not actually an array. Since we can clearly see that the text string is a text string then that only leaves the array variable somehow not being an array.

So we need to check the array variable's type to see if it is an array before we try to implode it. How do you suppose you might go about doing that?

Check google and PHP.net and see if you can come up with some ideas. Post back here when you have something.

I've honestly been trying my best with my limited knowledge and I can't afford to learn the whole language for such a small issue. After searching, this is still all I can come up with. I know that the type of input is "checkbox" but am I supposed to define $features as an array somehow? My old Actionscript days are probably confusing me even more. The only other thing I could bring to the table is maybe the problem is that one of the values contains a hyphen and that may be messing it up.

Rowsdower!
11-30-2011, 02:29 PM
I've honestly been trying my best with my limited knowledge and I can't afford to learn the whole language for such a small issue. After searching, this is still all I can come up with. I know that the type of input is "checkbox" but am I supposed to define $features as an array somehow? My old Actionscript days are probably confusing me even more. The only other thing I could bring to the table is maybe the problem is that one of the values contains a hyphen and that may be messing it up.

Seriously? Read what I wrote:


...So we need to check the array variable's type...

...Check google and PHP.net...

We're talking about a variable being passed as an argument in a PHP function. You need to check that variable's type (the PHP variable - the one that NEEDS TO BE AN ARRAY FOR THE FUNCTION TO WORK). We're not checking the HTML input element's type. This is a PHP issue so we're checking PHP variables. Come on, already!

I honestly don't believe you are trying at all but in a last-ditch effort I will offer this advice:

Google this exact phrase: "php check if variable is array"

The fact that you couldn't even get this far makes me all but convinced that you're just being lazy and asking for free work to do things you don't give a crap about doing yourself.

Or maybe I'm just having a bad day already...

Anyway, those search results will provide you with some elementary steps to take. Perhaps even the very first of those links would be relevant to your needs? Maybe it's staring you right in the face if you actually LOOK AT IT?

And being dramatic by suggesting that this requires learning "the whole language" is just annoying. I hate that attitude in children. It sends me over the edge in adults. A trained monkey could do this step. The only thing that takes any thought at all is figuring a good strategy for implementing the check but even that isn't lengthy (or difficult, or language-specific) to work out. It is just a matter of applying logic. Please take some initiative with this if you want me to take you seriously at all.

</grouchiness>

Geez... Maybe I just shouldn't have read your post first-thing today... :mad:

I'm going to go get some caffeine.

cineweekly.com
12-01-2011, 06:04 AM
Wow. Please don't ever be a teacher, you don't have the patience for it. The thing about a trained monkey is that it's been TRAINED. I've never been taught PHP. This is all I need it for.

Facts:
1. I don't know PHP... at all.
2. I'm trying my best.
3. I always research before asking or following-up.
4. I personally learn best by seeing the final result and then breaking down why it works.

I didn't realize you were dropping hints instead of telling me the answer so it's not my fault I didn't search for that exact phrase before. I did search for "php define variable as array" and that's how I came up with my response.

This time, I found bool is_array ( mixed $var) and changed 'var' to my variable 'features' then I'm totally lost. I try to find a section to learn more about but that leads me to 5 news things I don't know and it's an endless Catch-22.

You're seeing it from experienced eyes and it looks obvious to you but to someone that's never seen it, it's gibberish. And like I said, I dealt with variables in ActionScript years ago and that makes it doubly confusing. All I need is literally just 10 lines of code or so, I'm not asking for a whole video game to be programmed. I give back on these forums when I can contribute.

EDIT: Also, I posted this two weeks ago. I obviously have tried because I would much rather learn the answer on my own and move on than have to wait two weeks to be given the answer because of laziness.

Rowsdower!
12-01-2011, 10:25 PM
Don't worry, I'm in no danger of becoming a teacher. I have to ask, though: If you didn't think that I was trying to lead you to the right answer then what on earth did you think my post meant?

You know the expression "You can lead a horse to water but you can't make him drink?" Well this is the part we're on right now. From here you either drink or you don't.

Yes, is_array() is the function you will need to use. That's the PHP-specific part. In fact, it's the ONLY PHP-specific part about this step. Now having read the documentation on that very easy-to-use function you should have no trouble with any of the PHP involved. Which your last post confirms. You popped your variable's name into the function call which is all you need to do to make the function call operate properly. When executed it returns boolean values (true/false).

Now you can disengage your PHP mind and just think in basic logic and coding practices. In other words, you don't have to learn ANY more PHP for this (much less the "entire" language). If you can write Actionscript you can figure out how to use a conditional check to either do one thing if true or do something else if false. That's all you need to do now.

You need to use the result of your call to is_array() to decide whether or not to implode the variable. You already have all of the code pieces you need in order to do this. You just need to rearrange some things and use an "if" statement to alter the variable before it is added to the message string.

For heaven's sake TRY SOME THINGS and post back if you need further guidance. There is more than one "right" answer for this step. Even if you need to talk through the logic step by step without specific reference to code feel free to post it as a discussion.

I want you to use your own brains for this. I'm not budging any further on handouts.

cineweekly.com
12-01-2011, 11:08 PM
Please understand that I'm not looking for handouts and I am trying. I can't state this any more clearly. I said that my Actionscript days were YEARS ago and not highly advanced even in their prime. I knew you were trying to lead me somewhere but how should I know which exact phrase to pick out?

Here's the thing, you said the is_array () function is what I need but that's different from bool is_array ( mixed $features) When I put in the bolded section it didn't do anything so I figured there need to be some kind of rearrangement like the "if" statement you mentioned but I don't have a clue on how to do that. Maybe you need to see this from my point of view, here's what I'm playing with based directly from that php.net page:


$features = array('this', 'is', 'an array');

echo is_array($features) ? 'Array' : 'not an Array';

I don't know what to put in place of the wordsin single quotes and why isn't bool in front of is_array if you said bool is_array was correct? That's why I'm confused and you can't blame me for that or say I'm not trying.

I don't know what you have against just giving me the answer and letting me reverse engineer it especially since I told you that's how I learn. You may learn differently but that shouldn't mean I have to learn that way too. Also, we could have been done with this a long time ago if you'd just give me code that I could copy and paste and then learn from. I'm honestly offended that you say I'm not trying to just wanting spoon-fed code. I just don't understand the intense hate.

Rowsdower!
12-02-2011, 03:41 PM
Please understand that I'm not looking for handouts...I don't know what you have against just giving me the answer and letting me reverse engineer it...I'm honestly offended that you say I'm not trying to just wanting spoon-fed code...

Um...Isn't that a handout? Didn't you just prove me right? You can tell me you're offended all day long, but this is exactly what you are asking for. You're either lying to me or you're lying to yourself.

Either way, I'm not budging until you put something of value forward.


Also, we could have been done with this a long time ago if you'd just give me code that I could copy and paste and then learn from

Yep, and you could have been done a long time ago if you spent as much time reading (and maybe even thinking) as you have spent on complaining, begging, and making excuses. I don't feel bad for you in the least. Learn to code or hire the work out. I can't state that any more clearly.

I am honestly offended that you think you are entitled to free code from me or from anyone else. Offended. You are being offensive. And we all know that is a crime worse than any other. Offender. :rolleyes:

cineweekly.com
12-03-2011, 12:30 AM
You could have just given me the exact code and I could:
A) (Spoon-fed) Copy and paste the code without ever learning from it or looking at it.
B) (The option I proposed) Study how the code works so that I can better use it and know more about PHP in order to help another user in the future.

Now what's wrong with option B? The end result is the same as your "long way around" method except I can learn this way. Others learn differently from you, get over it. You want to play a game just because you feel superior for knowing the answer and you're beating me up about it. I tried and it makes no sense to me. Get over it. I don't have hours to waste on a computer language I only need to use on one page. You're seeing this whole thing from biased eyes and unfairly being an asshole.

I don't want your help. You're not worth the time. Don't respond, I'll get help somewhere else.

Rowsdower!
12-05-2011, 03:58 PM
You could have just given me the exact code and I could:
A) (Spoon-fed) Copy and paste the code without ever learning from it or looking at it.
B) (The option I proposed) Study how the code works so that I can better use it and know more about PHP in order to help another user in the future.

Now what's wrong with option B? The end result is the same as your "long way around" method except I can learn this way. Others learn differently from you, get over it. You want to play a game just because you feel superior for knowing the answer and you're beating me up about it. I tried and it makes no sense to me. Get over it. I don't have hours to waste on a computer language I only need to use on one page. You're seeing this whole thing from biased eyes and unfairly being an asshole.

I don't want your help. You're not worth the time. Don't respond, I'll get help somewhere else.

If you operate under "option B" then I can't help but be amazed that you already understand everything else in your contact form script, yet you don't understand how to use is_array() in an "if" statement. I am also amazed that you point out quite plainly that you don't plan to invest any time learning PHP, and in the same breath tell me that you're planning to dissect any code provided in order to learn how it works.

You are a mountain of inconsistencies. Your track record does not support your arguments. You have been exposed.

Good day and good luck to you.

cineweekly.com
12-05-2011, 11:19 PM
if you operate under "option b" then i can't help but be amazed that you already understand everything else in your contact form script, yet you don't understand how to use is_array() in an "if" statement. I am also amazed that you point out quite plainly that you don't plan to invest any time learning php, and in the same breath tell me that you're planning to dissect any code provided in order to learn how it works.

You are a mountain of inconsistencies. Your track record does not support your arguments. You have been exposed.

Good day and good luck to you.

tldr.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum