Syrehn Posted December 16, 2009 Share Posted December 16, 2009 I am posting this for a friend of mine, i wrote a small script for her to process her form data to an email but in testing i am having trouble getting the checkbox data to display in the email it sends. if you select multiple checkboxes it emails only the last checked box. the following is the html code: <td><p>Job Description:</p></td> <td><label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Drywall" id="jobdescription_0" /> Drywall</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Insulation" id="jobdescription_1" /> Insulation</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Taping" id="jobdescription_2" /> Taping</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Texture" id="jobdescription_3" /> Texture</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Stucco" id="jobdescription_4" /> Stucco</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="T-Bar" id="jobdescription_5" /> T-Bar</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Custom Ceilings" id="jobdescription_6" /> Custom Ceilings</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Steel Stud Framing" id="jobdescription_7" /> Steel Stud Framing</div> <div align="left"></div> <label> </label> <div align="left"> <input type="checkbox" name="job_description[]" value="Bulk Heads" id="jobdescription_8" /> Bulk Heads</div> and this is the php code: <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "test@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $body = "Name: $_REQUEST $name \n Email Address: $_REQUEST $email \n Phone: $_REQUEST $phone \n Job Type: $_REQUEST $job_type \n Job Description: $_REQUEST $job_description \n"; $timestamp = date("n/j/Y, \a\\t g:i a T"); $thankyousubject = "Request Submitted Successfully \n on \n $timestamp"; //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you $_REQUEST[name] we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. $job_description =""; foreach($_REQUEST["job_description"] as $value) { $job_description .= "$value\n"; } ?> <?php mail($yourEmail, $subject, $body, "From: $_REQUEST[$name] < $_REQUEST[$email] >"); print "<CENTER><B><body bgcolor='#2c2c2c'><font color='#FFFFFF'> $thankyousubject </B></CENTER><br><br><center>$thankyoumsg</center><BR><BR><CENTER><br><br></font><A HREF=\"http://www.shekinteriors.com\" TARGET=\"_top\"><IMG SRC=\"http://www.shekinteriors.com/formbanner.gif\" WIDTH=\"600\" HEIGHT=\"100\" ALT=\"Return to Shek Interiors Ltd\" BORDER=\"0\"></A></CENTER></body>" ?> i thought the $job_description =""; foreach($_REQUEST["job_description"] as $value) { $job_description .= "$value\n"; } would help this but the mail it generates looks like the following: Name: Array test525 Email Address: Array dtyjdtj@ftdhdxf.com Phone: Array 67586785 Job Type: Array New Commercial Job Description: Array Bulk Heads could someone please help me figure this out. i'm not super familiar with php and it's driving me bonkers! lol Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 16, 2009 Share Posted December 16, 2009 Yes, checkboxes are a bit of a hassle. I think your problem is with the square brackets in the checkbox names, so I would name them "job_description_1", "job_description_2", etc. Then retrieve the values with something like: foreach($_POST as $tmp_ind => $tmp_val) { if(preg_match("/^job_description_/", $tmp_ind)) $job_description .= $tmp_val . "\n"; } Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 16, 2009 Author Share Posted December 16, 2009 Yes, checkboxes are a bit of a hassle. I think your problem is with the square brackets in the checkbox names, so I would name them "job_description_1", "job_description_2", etc. Then retrieve the values with something like: foreach($_POST as $tmp_ind => $tmp_val) { if(preg_match("/^job_description_/", $tmp_ind)) $job_description .= $tmp_val . "\n"; } Oh alright. I will try that. I was under the impression that the [] were necessary. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 16, 2009 Share Posted December 16, 2009 your email looks like that because in the $body variable, you have $_REQUEST scattered around, which is a global array (and should not be used, IMO. use $_POST or $_GET respectively). checkboxes are no more difficult to use than regular form data. your code will work, you just don't have it setup properly. you have this: $job_description =""; foreach($_REQUEST["job_description"] as $value) { $job_description .= "$value\n"; } which will populate $job_description with each value of the checkbox array, but you do nothing with it. you need to put that variable in the $body var as well. so, place that chunk of code above where you create the $body var, fix your $_REQUEST issues like so: $body = "Name: $_POST['name'] \n Email Address: $_POST['email'] ... etc. and try again. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 16, 2009 Share Posted December 16, 2009 Yes, checkboxes are a bit of a hassle. I think your problem is with the square brackets in the checkbox names, so I would name them "job_description_1", "job_description_2", etc. Then retrieve the values with something like: foreach($_POST as $tmp_ind => $tmp_val) { if(preg_match("/^job_description_/", $tmp_ind)) $job_description .= $tmp_val . "\n"; } [] square brackets create the array which is the best possible way of working with checkboxes, and similar items. keep the [] brackets there, and continue with what i was saying in my last post. you have the code, you were just using it incorrectly. Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 16, 2009 Share Posted December 16, 2009 [] square brackets create the array which is the best possible way of working with checkboxes, and similar items. keep the [] brackets there, and continue with what i was saying in my last post. you have the code, you were just using it incorrectly. Ah, I remember experimenting with the square brackets a few years ago and found they either didn't work or were unreliable (can't remember which!) – so I've avoided them ever since. I'll have to give them another go. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 16, 2009 Share Posted December 16, 2009 EDIT: Looks like Marcus already addressed the problem, but I'll keep this anyway since the code below may be useful Yes, checkboxes are a bit of a hassle. I think your problem is with the square brackets in the checkbox names, so I would name them "job_description_1", "job_description_2", etc. That's just silly. The brackets make the fields an array of values. By hard-coding different names the code is notflexible. I tried your code and the values for the job descriptions were getting handled correctly. Your problem is that you define the $body before you define $job_description! The $job_description variable you define in the foreach() loop is never used. And, instead of a loop for the array, just use implode(). Change the order of the logic and it will work fine. You also have some problems with the HTML. There is a bold and center tags before the body tag. Plus, font tags have been depricated for years. <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "test@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $job_description = implode(', ', $_REQUEST["job_description"]); $body = "Name: $_REQUEST $name \n"; $body .= "Email Address: $_REQUEST $email\n"; $body .= " Phone: $_REQUEST $phone \n"; $body .= "Job Type: $_REQUEST $job_type \n"; $body .= "Job Description: $job_description \n"; mail($yourEmail, $subject, $body, "From: $_REQUEST[$name] < $_REQUEST[$email] >"); $timestamp = date("n/j/Y, \a\\t g:i a T"); $thankyousubject = "Request Submitted Successfully \n on \n $timestamp"; //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you $_REQUEST[name] we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. ?> <html> <body style="background-color:#2c2c2c;color:#FFFFFF"> <center> <b><?php echo $thankyousubject; ?></b> <br /><br /> <?php echo $thankyoumsg; ?> <br /><br /><br /><br /> <A HREF=\"http://www.shekinteriors.com\" TARGET=\"_top\"> <IMG SRC=\"http://www.shekinteriors.com/formbanner.gif\" WIDTH=\"600\" HEIGHT=\"100\" ALT=\"Return to Shek Interiors Ltd\" BORDER=\"0\"> </A> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 16, 2009 Author Share Posted December 16, 2009 EDIT: Looks like Marcus already addressed the problem, but I'll keep this anyway since the code below may be useful That's just silly. The brackets make the fields an array of values. By hard-coding different names the code is notflexible. I tried your code and the values for the job descriptions were getting handled correctly. Your problem is that you define the $body before you define $job_description! The $job_description variable you define in the foreach() loop is never used. And, instead of a loop for the array, just use implode(). Change the order of the logic and it will work fine. You also have some problems with the HTML. There is a bold and center tags before the body tag. Plus, font tags have been depricated for years. <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "test@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $job_description = implode(', ', $_REQUEST["job_description"]); $body = "Name: $_REQUEST $name \n"; $body .= "Email Address: $_REQUEST $email\n"; $body .= " Phone: $_REQUEST $phone \n"; $body .= "Job Type: $_REQUEST $job_type \n"; $body .= "Job Description: $job_description \n"; mail($yourEmail, $subject, $body, "From: $_REQUEST[$name] < $_REQUEST[$email] >"); $timestamp = date("n/j/Y, \a\\t g:i a T"); $thankyousubject = "Request Submitted Successfully \n on \n $timestamp"; //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you $_REQUEST[name] we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. ?> <html> <body style="background-color:#2c2c2c;color:#FFFFFF"> <center> <b><?php echo $thankyousubject; ?></b> <br /><br /> <?php echo $thankyoumsg; ?> <br /><br /><br /><br /> <A HREF=\"http://www.shekinteriors.com\" TARGET=\"_top\"> <IMG SRC=\"http://www.shekinteriors.com/formbanner.gif\" WIDTH=\"600\" HEIGHT=\"100\" ALT=\"Return to Shek Interiors Ltd\" BORDER=\"0\"> </A> </center> </body> </html> do you mind if i ask why you put the $body like this vs how i had it? : $body = "Name: $_REQUEST $name \n"; $body .= "Email Address: $_REQUEST $email\n"; $body .= " Phone: $_REQUEST $phone \n"; $body .= "Job Type: $_REQUEST $job_type \n"; $body .= "Job Description: $job_description \n"; Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 16, 2009 Share Posted December 16, 2009 That's just silly. The brackets make the fields an array of values. By hard-coding different names the code is notflexible. Doh! It's perfectly flexible. I've used this method in my own CMS for years without any problems : ) Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 16, 2009 Share Posted December 16, 2009 do you mind if i ask why you put the $body like this vs how i had it? : Readability. No functional difference whatsoever. Having it all on one line made it difficult to read because you have to scroll back and forth to see it all. Plus, the modified structure presents it similar to how it would be displayed in the email. That's just silly. The brackets make the fields an array of values. By hard-coding different names the code is notflexible. Doh! It's perfectly flexible. I've used this method in my own CMS for years without any problems : ) Really? Using RegEx to try and ascertain the correct fields to pull values from assumes that when there are changes to be made later (by the same or different person) that the person knows the logic for ascertaining those fields. It could happen that they want to add a field for the user to enter the number of years that they have conducted those job descriptions. That field might be created with the name "job_description_years" - which would introduce a bug. Of course, I am manufacturing a scenario that would break that code and lead to additional time needed to debug. But, the point is that it is not an efficient method and introduces much more complexity than simply making the fields an array and using a single implode() function in the processing page. Note to OP: you might also consider making an array of the job descriptions and creating a small loop to generate your form code. Somethig like this: $descriptionsAry = ( 'Drywall', 'Insulation', 'Taping', 'Texture', 'Stucco', 'T-Bar', 'Custom Ceilings', 'Steel Stud Framing', Bulk Heads ); $formFields = ''; foreach ($descriptionsAry as $id => $value) { $formFields .= "<div align=\"left\">\n"; $formFields .= "<input type=\"checkbox\" name=\"job_description[]\" value=\"{$value}\" id=\"jobdescription_{$id}\" />\n"; $formFields .= "<label for="jobdescription_{$id}">{$value}</label>\n"; $formFields .= "</div>\n"; } Then in the form: <form method="post" action="" > <td><p>Job Description:</p></td> <td> <?php echo $formFields ; ?> </td> This ensures all the fields are created consistently. There are currently some minor problems with the form code (e.g. empty divs, labels not associated with the fields, etc.). by properly associating a label with the field, the user can click on the label to activate the field. Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 16, 2009 Author Share Posted December 16, 2009 This ensures all the fields are created consistently. There are currently some minor problems with the form code (e.g. empty divs, labels not associated with the fields, etc.). by properly associating a label with the field, the user can click on the label to activate the field. Yeah, I saw the issues. I didn't write the html code. Only the php. I might help her clean up the html side a bit and let her know about that. Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 16, 2009 Share Posted December 16, 2009 That's just silly. The brackets make the fields an array of values. By hard-coding different names the code is notflexible. Doh! It's perfectly flexible. I've used this method in my own CMS for years without any problems : ) Really? Using RegEx to try and ascertain the correct fields to pull values from assumes that when there are changes to be made later (by the same or different person) that the person knows the logic for ascertaining those fields. It could happen that they want to add a field for the user to enter the number of years that they have conducted those job descriptions. That field might be created with the name "job_description_years" - which would introduce a bug. Of course, I am manufacturing a scenario that would break that code and lead to additional time needed to debug. But, the point is that it is not an efficient method and introduces much more complexity than simply making the fields an array and using a single implode() function in the processing page. OK, that is a potential problem with my quickly written example (I didn't bother including my usual regex to only match numbers after the initial "job_description_"). On it's own that doesn't make this method inflexible, but I get your point, i.e. it's better to code things properly, especially if the code could be edited by someone else in the future. Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 ok, so the checkbox values are now showing up but it's emailing like this: Name: Array heather last Email Address: Array last@last.com Phone: Array 76767676 Job Type: New Commercial Job Description: Drywall, Insulation, Taping, Texture, Stucco what could be causing the array in the email, phone and name fields? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 17, 2009 Share Posted December 17, 2009 ok, so the checkbox values are now showing up but it's emailing like this: Name: Array heather last Email Address: Array last@last.com Phone: Array 76767676 Job Type: New Commercial Job Description: Drywall, Insulation, Taping, Texture, Stucco what could be causing the array in the email, phone and name fields? i already addressed this in my first post. instead of: $_REQUEST $name do: $_POST['name'] and so on for each. Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 Ah, I was just going with the sample to test that mjdamato posted and it didn't show them changed there. I'm trying the $_POST now. this is the code that i have now: <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "shekinteriors@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $job_description = implode(', ', $_POST["job_description"]); $body = "Name: $_POST $name \n"; $body .= "Email Address: $_POST $email \n"; $body .= " Phone: $_POST $phone \n"; $body .= "Job Type: $_POST $job_type \n"; $body .= "Job Description: $job_description \n"; mail($yourEmail, $subject, $body, "From: $_POST[$name] < $email >"); $timestamp = date("n/j/Y, \a\\t g:i a T"); $thankyousubject = "Request Submitted Successfully \n on \n $timestamp"; //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you $_POST[name] we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. ?> <html> <body style="background-color:#2c2c2c;color:#FFFFFF"> <center> <b><?php echo $thankyousubject; ?></b> <br /><br /> <?php echo $thankyoumsg; ?> <br /><br /><br /><br /> <A HREF=\"http://www.shekinteriors.com\" TARGET=\"_top\"> <IMG SRC=\"http://www.shekinteriors.com/formbanner.gif\" WIDTH=\"600\" HEIGHT=\"100\" ALT=\"Return to Shek Interiors Ltd\" BORDER=\"0\"> </A> </center> </body> </html> i had to change the mail line to: mail($yourEmail, $subject, $body, "From: $_REQUEST[$name] < $email >"); to get it to generate the email address otherwise in the from line it was not generating the email just invalid syntax. even with the above changes i'm still getting an array issue. emails are coming to me like this: Name: Array last heather Email Address: Array test@hotmail.com Phone: Array 1111111 Job Type: Array New Commercial Job Description: Drywall, Insulation, Taping, Texture, Stucco, T-Bar am i still missing something? i appreciate all the help! Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 17, 2009 Share Posted December 17, 2009 Thought I should help since I contributed to making a bit of a mess of your thread... There were two main issues with your code which should have been resolved in the code below: [*]A number of the $_POST values were messed up, e.g. $_POST $name should be $_POST["name"]. [*]I think it's always a good idea to separate variables from normal text by concatenating strings, e.g. I've changed "From: $_POST[$name] < $email >" to "From: " . $_POST["name"] . "<" . $_POST["email"] . ">". This just keeps your variables separate, so they can't get confused with the normal text. I've also made a couple of other very minor changes just to make the PHP a little more compact. <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "shekinteriors@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $body = "Name: " . $_POST["name"] . "\n"; $body .= "Email Address: " . $_POST["email"] . "\n"; $body .= "Phone: " . $_POST["phone"] . "\n"; $body .= "Job Type: " . $_POST["job_type"] . "\n"; $body .= "Job Description: " . implode(", ", $_POST["job_description"]) . "\n"; mail($yourEmail, $subject, $body, "From: " . $_POST["name"] . "<" . $_POST["email"] . ">"); $thankyousubject = "Request Submitted Successfully \n on \n " . date("n/j/Y, \a\\t g:i a T"); //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you " . $_POST["name"] . " we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. ?> <html> <body style="background-color:#2c2c2c;color:#FFFFFF"> <center> <b><?php echo $thankyousubject; ?></b> <br /><br /> <?php echo $thankyoumsg; ?> <br /><br /><br /><br /> <a href="http://www.shekinteriors.com" target="_top"> <img src="http://www.shekinteriors.com/formbanner.gif" width="600" height="100" alt="Return to Shek Interiors Ltd" border="0"> </a> </center> </body> </html> Give that a go and let me know if you encounter any further problems. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 17, 2009 Share Posted December 17, 2009 Please read mrMarcus's post again! Where did you lear to reference array values using $arrayVar $indexName ? In fact, you shouldn't even be getting the values at all since I don't see anywhere that $email, $phone or $job_type have been defined. Looks like extract() is being run against the $_POST variable, but it's not in your code. Are you sure that is all the code? Anyway, try this: <?php date_default_timezone_set('America/Edmonton'); $yourEmail = "shekinteriors@yahoo.ca"; //The Email Address Form Results Will Be Sent To. Change to your email. $subject = "Online Job Requests"; //This will be displayed in your Email Subject line. Edit as needed. $job_description = implode(', ', $_POST["job_description"]); $body = "Name: {$_POST['name']} \n"; $body .= "Email Address: {$_POST['email']} \n"; $body .= " Phone: {$_POST['phone']} \n"; $body .= "Job Type: {$_POST['job_type']} \n"; $body .= "Job Description: {$job_description} \n"; mail($yourEmail, $subject, $body, "From: {$_POST['name']} < {$_POST['email']} >"); $timestamp = date("n/j/Y, \a\\t g:i a T"); $thankyousubject = "Request Submitted Successfully \n on \n $timestamp"; //This will be displayed after a submission. Edit to suit your needs. The \n represent page breaks. $thankyoumsg = "Thank you {$_POST['name']} we have recieved your information. We will contact you soon."; //This will be displayed after a submission. Edit to suit your needs. ?> <html> <body style="background-color:#2c2c2c;color:#FFFFFF"> <center> <b><?php echo $thankyousubject; ?></b> <br /><br /> <?php echo $thankyoumsg; ?> <br /><br /><br /><br /> <a HREF=\"http://www.shekinteriors.com\" TARGET=\"_top\"> <IMG SRC=\"http://www.shekinteriors.com/formbanner.gif\" WIDTH=\"600\" HEIGHT=\"100\" ALT=\"Return to Shek Interiors Ltd\" BORDER=\"0\"> </a> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 I really appreciate all your help everyone. I will try this. I'm so new to PHP anything I have learned I learned in school last year. I'm learning alot from this thread and your help! I also didn't realize until your repost that I didn't put the [' '] around some of the body stuff. DOH! I'm sorry! Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 I'm sorry for all my silly questions but do you mind if I ask what the { in the body before and after the $_POST['variable'] does vs not having it there? Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 Also, and I'm probably wrong, but i was under the impression that when you have something like $_POST['email'] i was told that the ' ' inside the [] were not necessary? And I'm just curious mjdamato how come you have " " in come of the $_POST and ' ' in others? Thanks for taking the time with all my questions I really appreciate it Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 17, 2009 Share Posted December 17, 2009 Also, and I'm probably wrong, but i was under the impression that when you have something like $_POST['email'] i was told that the ' ' inside the [] were not necessary? And I'm just curious mjdamato how come you have " " in come of the $_POST and ' ' in others? Thanks for taking the time with all my questions I really appreciate it they are "not necessary" in that $_POST[name] will work, however, name without quotes is not a string in how you are intending, but an undefined constant. PHP automatically corrects your error to a literal string by wrapping name with quotes 'name'. so, either way, your quotes will be inserted. how you're doing it, however, is causing PHP to do a little extra work. always use quotes when addressing a literal string. Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 17, 2009 Author Share Posted December 17, 2009 Also, and I'm probably wrong, but i was under the impression that when you have something like $_POST['email'] i was told that the ' ' inside the [] were not necessary? And I'm just curious mjdamato how come you have " " in come of the $_POST and ' ' in others? Thanks for taking the time with all my questions I really appreciate it they are "not necessary" in that $_POST[name] will work, however, name without quotes is not a string in how you are intending, but an undefined constant. PHP automatically corrects your error to a literal string by wrapping name with quotes 'name'. so, either way, your quotes will be inserted. how you're doing it, however, is causing PHP to do a little extra work. always use quotes when addressing a literal string. Ok gotcha! I understand. TY! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2009 Share Posted December 18, 2009 I'm sorry for all my silly questions but do you mind if I ask what the { in the body before and after the $_POST['variable'] does vs not having it there? As you may know, when working with strings delimited with double quotes you can put variables within the string and PHP will interpret the variable into it's value. It does not do this with strings delimited with single quotes. Example: $name = "Syrehn"; echo "Hello $name"; //Output = Hello Syrehn echo 'Hello $name'; //Output = Hello $name However, there are times when PHP can't decypher where the string begins or ends or, with array values, the quotes surrounding the index name can cause the string to be terminated instead of being parsed. But, you can enclose variables within curly braces inside double quoted strings to ensure the PHP parser understands exactly where the variable is. Example: $spec = '2.2'; echo "The PC is $specghz"; //Output = The PC is //echo the var $specghz doesn't exist! echo "The PC is {$spec}ghz"; //Output = The PC is 2.2ghz Also, and I'm probably wrong, but i was under the impression that when you have something like $_POST['email'] i was told that the ' ' inside the [] were not necessary? And I'm just curious mjdamato how come you have " " in come of the $_POST and ' ' in others? Thanks for taking the time with all my questions I really appreciate it As mrMarcus already stated, if you don't use quotes, PHP will try to fix it. So, it is not good practice to leave them off. As to why I used double quotes in one instance and single quotes in another was just me missing one pair. You can use either, but I typically prefer to use single quotes for text which is not dynamic and use double quotes when I want to use variables to be parsed (as explained above) Quote Link to comment Share on other sites More sharing options...
Syrehn Posted December 19, 2009 Author Share Posted December 19, 2009 Thank you so much for all your help. And for taking the time to answer all my questions. I really have learned alot during this process. The processor is working now and I appreciate everyone's responses! I'm going to mark this thread as solved. Happy Holidays! Thank you again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.