janekoo
-
Posts
9 -
Joined
-
Last visited
Posts posted by janekoo
-
-
I understand the concept now. Thank you very much, HartleySan.
-
Thank you Hartlysen. I just need some clarification to make sure I understand what you are saying.
1) The double quote statement you brought up at the end will do the same thing as the single quote statement. ( For me personally, the double quote method statement is much easier to understand.)
Single quote: echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;
Double quote : echo "<input type=\"checkbox\" name=\"sp[]\" value=\"$k\"> $v";
2) If I were to use the double quote method in my form script, can i still use the following in my form processing script? It works if I use the single quote method.
include('checkbox_data.php');
if (isset($_POST['sp'])) {
foreach ($_POST['sp'] as $v) {$bodysp .='<br>' . $sp[$v];}
}3) $k is a variable but is part of the '<input type ...>' string. So we use single quote in the value section of the '<input type...>' string. In fact, re: value = "'. $k. '" - we have two strings.- the outer double quote string showing what should be contained in the value part of the input type string, and the inner single quote string encompassing the $k. Since we have two strings, we need 2 concatenate operators surrounding the $k - we need them to connect the outer and the inner string.
4) $v is a variable and is not part of the '<input type ...>' string. We only need 1 concatenate operator to connect it with the '<input type ...>' string.
-
Hello,
Thanks to HartleySan and Margaux, I was able to solve my problem. However, instead of just copying the codes HartleySan gave me, I want to understand what I am doing. So I hope someone could enlighten me.
To recap, I created 4 product categories using checkbox arrays.
In the beginning, I followed the array's chapter in the book and created the following code for my SP category in my form script.
<input type="checkbox" name="SP[]" value="SP100" /> S&P 100 Fund
<input type="checkbox" name="SP[]" value="SP200" /> S&P 100 Fund
and the following code for my SP category in my form processing script
if (isset($_POST['SP']) AND is_array($_POST['SP'])) {
foreach ($_POST['SP'] as $SP) {
print " $SP <br>\n ";
HartleySan suggested that I have a separate php file for the arrays and then include it in both my form script and my form processing script.
checkbox_data.php
<?php
$sp = array(1 => 'S&P 100 Fund', 'S&P 200 Fund', 'S&P 300 Fund');
// Other arrays here .
That works great by the way. My only problem is I don't understand the codes he suggested as to how to echo the checkbox array in my form script:
foreach ($sp as $k => $v) {
echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;
}
He wrote: "That way, the array keys would become the values for the posted data."
For the array named $SP, I understand that $k refers to the index so in this case, will be 1, 2, 3, and so on. $v refers to the value, on in this case, will be S&P 100 Fund, S&P 200 Fund and so on,
it is the echo statement that I have problems with. I understand name should be "sp[]" in order to create the $_POST('sp') array, but I don't understand the value part. value = "'.$k. '" ?? I understand the value should be enclosed in a pair of double quotes, but
1) Why is there a pair of single quotes enclosing .$k.? 2) Why are there 2 periods before and after $k? i.e. (. $k .) Is that used to echo $k one by one e.g. 1 2 3 and so on?
3) As to ( ' . $v)? I understand the ' is to indicate the closing of the echo statement, but can the ' be placed after $v i.e.( .$v' )instead?? In other words, the echo statement does not encompass the .$v??
4) Finally, what is the period before $v ? Is that used to echo $v one by one e.g. S&P 100 Fund, S&P 200 Fund, What is the difference between 1 period before the $v and 2 periods before and after the $k?
I know the codes work, but I would appreciate a brief explanation on how it works. Thank you very much.
-
Thank you margaux, after I put in the isset code, I did not get the warning anymore. Thanks for your help.
-
Hello Hartlysan,
I need your help. I followed your instructions and created a checkbox_data.php. Codes are as follows:
<?php$sp = array(1 => 'S&P 100 Fund', 'S&P 200 Fund', 'S&P 300 Fund', 'S&P 400 Fund' ,'S&P 500 Fund');$nd = array(1 => 'ND 100 Fund', 'ND 200 Fund', 'ND 300 Fund', 'ND 400 Fund' ,'ND 500 Fund');$dj = array(1 => 'DJ 100 Fund', 'DJ 200 Fund', 'DJ 300 Fund', 'DJ 400 Fund' ,'DJ 500 Fund');$ru = array(1 => 'RU 100 Fund', 'RU 200 Fund', 'RU 300 Fund', 'RU 400 Fund' ,'RU 500 Fund');?>Then I added the include ('checkbox_data.php) script and the foreach codes for each of my 4 product categories in my form script.
<?phpinclude('checkbox_data.php');?><form action="handle_Cat4Form_mailprint.php" method="post" ;><br><p style="font-weight:bold;"> Check all products that you want:</p><br><p style="font-weight:bold;" >S&P Equity Funds</p><br><?phpforeach ($sp as $k => $v) {echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;}?><br> <br><p style = "font-weight:bold;">Nasdaq Equity Funds</p><br><?phpforeach ($nd as $k => $v) {echo '<input type="checkbox" name="nd[]" value="' . $k . '"> ' . $v;}?><br> <br><p style = "font-weight:bold;">Dow Jones Equity Funds</p><br><?phpforeach ($dj as $k => $v) {echo '<input type="checkbox" name="dj[]" value="' . $k . '"> ' . $v;}?><br> <br><p style="font-weight:bold;" >Russell Equity Funds</p><br><?phpforeach ($ru as $k => $v) {echo '<input type="checkbox" name="ru[]" value="' . $k . '"> ' . $v;}?>Then in my form processing script, I have<?phperror_reporting (E_ALL ^ E_NOTICE);session_start();?><html><head><title> Handle Cat4 Form</title></head><body><?php //this will email the data from Cat1Form.php to Indexcellent and print user confirmation.include('checkbox_data.php');foreach ($_POST['sp'] as $v) {$bodysp= 'SP selection: ' . $sp[$v];}foreach ($_POST['nd'] as $v) {$bodynd= 'ND selection: ' . $nd[$v];}foreach ($_POST['dj'] as $v) {$bodydj= 'DJ selection: ' . $dj[$v];}foreach ($_POST['ru'] as $v) {$bodyru= 'RU selection: ' . $ru[$v];}When I selected products from only SP, and ND and NOT DJ and RU, the following warning is displayed:Note: line 17 is the foreach code for DJ and line 18 is the foreach code for RU.Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Indexcellent\handle_Cat4Form_mailprint.php on line 17
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Indexcellent\handle_Cat4Form_mailprint.php on line 18If I selected all 4 categories, no warning.How do I get rid of the warnings?One more question. foreach ($_POST['sp'] as $v) {$bodysp= 'SP selection: ' . $sp[$v];}Is it $bodysp or $bodysp. (is the period neccessary after bodysp?)
Thanks.
Jane -
Hello HartleySan,
Thank you for your reply. Just one question - where should I place the include script in my script?
include('checkbox_data.php');
Should it go to the <head>tag section or the <body> tag section?
In the <head> section, I have a <title> tag, a <link> tag to a CSS file, and a <script> tag which is a piece of javascript to make sure user enters username. I don't think the include script should go there. If it should go to the head section, can I put it after the <script> tag?
In the <body> section, I have the form tag, should I place the script before the form tag or inside the form tag?? Or it does not matter.
Option 1- Before the form tag
<?php
include('checkbox_data.php');
?>
<form action="handle_CatForm.php" method="post" ;> <br>
<p style="font-weight:bold;"> Check all products that you want:</p><br> //Instructions<p style="font-weight:bold;" >S&P Equity Funds</p> <br> //SP category<?phpforeach ($sp as $k => $v) {
echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;}?><p style="font-weight:bold;" >ND Equity Funds</p> <br> //ND Category
<?phpforeach ($nd as $k => $v) {
echo '<input type="checkbox" name="nd[]" value="' . $k . '"> ' . $v;}?>Option 2- Inside the form tag
<form action="handle_CatForm.php" method="post" ;> <br>
<?phpinclude('checkbox_data.php');
?>
<p style="font-weight:bold;"> Check all products that you want:</p><br> //Instructions<p style="font-weight:bold;" >S&P Equity Funds</p> <br> //SP category<?phpforeach ($sp as $k => $v) {
echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;}?>Thanks.
Jane
-
Hello HarleySan,
Thank you so much for your help and suggestion.
I was able to revise my form script (with modification since I don't know how to include your checkbox_data.php). But I need help to revise my form processing script.
1. My form script (Catform.php)
I revised my form script as follows:
Since I don't know how to include your checkbox_data.php, I put the codes directly inside my form script. For example, in the SP category section, I put the codes (in red) immediately after the paragraph referring to the description of the category - S&P Equity Funds.
<p style="font-weight:bold;" >S&P Equity Funds</p> <br><?php$sp = array(1 => 'S&P 100 Fund', 'S&P 200 Fund', 'S&P 300 Fund', 'S&P 400 Fund', 'S&P 500 Fund');foreach ($sp as $k => $v) {echo '<input type="checkbox" name="sp[]" value="' . $k . '"> ' . $v;}?>Similarly for the next category (ND), I put the codes immediately after the paragraph with the description of the category - ND Equity Funds.
<p style = "font-weight:bold;">ND Equity Funds</p><br>
2. My form processing script (Handle_Catform.php) - Please help!
After I enter the following section (in blue) at the beginning of my script,
<?php //this will send the data from CatForm.php to my email address.
//Create variables:
$to = "my email address";
$username = $_POST['username'];
$comments= $_POST['comments']$headers = "From: $username";
how do I code the $body section if I don't include the checkbox_data.php?
I don't think there should be 4 $body sections but only one that incorporates/concatenates all the user input for each category plus $comments Can I have something like:
$body = $bodysp +$bodynd + $bodydj + $bpduru + $comment (what should be used instead of the + sign?)
Can I have:
$body = $bodysp + $bodynd + $bodydj + $bodyru + $comments
(what is the correct concatenating symbol to use in this case, I don't think it should be + sign).
$sp = array(1 => 'S&P 100 Fund', 'S&P 200 Fund', 'S&P 300 Fund', 'S&P 400 Fund', 'S&P 500 Fund');foreach ($_POST['sp'] as $v) {
$bodysp .= 'SP value: ' . $sp[$v];
}$nd = array(1 => 'ND100 Fund', 'ND 200 Fund', 'ND 300 Fund', 'ND 400 Fund', 'ND 500 Fund');
foreach ($_POST[''nd'] as $v) {
$bodynd .= 'ND value: ' . $nd[$v];
}$dj = array(1 => 'DJ100 Fund', 'DJ 200 Fund', 'DJ 300 Fund', 'DJ 400 Fund', 'DJ 500 Fund');
foreach ($_POST[''dj'] as $v) {
$bodydj.= 'DJ value: ' . $dj[$v];
}$ru = array(1 => 'RU100 Fund', 'RU 200 Fund', 'RU 300 Fund', 'RU 400 Fund', 'RU 500 Fund');
foreach ($_POST[''ru'] as $v) {
$body ru.= 'RU value: ' . $ru[$v];
}mail($to, $subject, $body, $headers);
Thanks.
Jane
-
Hello,
I have a file called Catform.php which allows users to 1) select products from 4 different product categories and 2) provide comments if they cannot find their product. This php file is linked to another file called handle_Catform.php which processes the user input and mail the input to my email address.
Catform.php
1) On the Catform.php, to link to the handle_Catform.php, I used the following code:
<form action="handle_Catform.php" method="post" ;>
2) For the 4 product categories, I arranged them using checkbox arrays. For example, for the SP category, I used the following code:
<p style="font-weight:bold;" >S&P Equity Funds</p> <br>
<input type="checkbox" name="SP[]" value="SP100" /> S&P 100 Fund
<input type="checkbox" name="SP[]" value="SP200" /> S&P 200 Fund
<input type="checkbox" name="SP[]" value="SP300" /> S&P 300 Fund
Then for the other 3 categories (ND, DJ and RU), I just replaced the name from "SP[]" to "ND[]" and so forth.3) For the user comment, I used textarea. I used the following code:
<p style = "font-weight:bold;"> Comments</p>
<textarea name="comments" rows="6" cols="60"></textarea>Handle_Catform.php
I had no problem capturing and printing the user input from Catform.php.
Below is an excerpt of my codes showing how I print input from the SP category and user input comments.
<?php //this will receive the data from CatForm.php.
//Create variables
$username = $_POST['username'];
$comments= $_POST['comments'];
//Print Greetings
print "Congratulations, $username! You have successfully made the following selections: <br> <br>";
//Print category SPif (isset($_POST['SP']) AND is_array($_POST['SP'])) {
foreach ($_POST['SP'] as $SP) {
print " $SP <br>\n ";
}
} else {
print'No selection for SP<br>';
}//Print comments
if (($_POST['comments'])!=""){
print " You provided the following comments: $comments <br>\n ";
}
else {
print'You did not enter any comments. <br>';
}?>
Question: How do I use the mail () function to send the same user input to my email address on Handle_Catform.php? Please help!
I tried the following codes but it did not work quite right and I don't know how to incorporate $comments in $body. I just need something like the following: For example, if the user selects only from SP category and provide comments " I want something else", then the email will look like:
"From [the username of the user]
We have received the following information:
SP Selection = whatever user selected
ND Selection =
DJ Selection =
RU Selection =
Comments = I want something else."
<?php //this will send the data from CatForm.php to my email address.
//Create variables:
$to = "my email address";
$username = $_POST['username'];
$comments= $_POST['comments'];
$from = $_POST['username']
$fields = array();
$fields{"SP"} = "SP Selection= ";
$fields{"ND"} = "ND Selection= ";
$fields{"DJ"} = "DJ Selection= ";
$fields{"RU"} = "RU Selection= ";
$subject = "Category Selections";
$headers = "From: $username";
$body = "We have received the following information:\n\n"; foreach($fields as $a => ${ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }mail($to, $subject, $message, $headers);
}
?>Thank you so much.
Jane
Redirect Header Function Works On Localhost But Not Under Yahoo Hosting
in PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
Posted
Hello,
Please help! Thank you.
I have a webpage (a member log in page called logcheck.php) which allows members to enter their member name and password, then once the member name (member_name) and password (pass) are validated, i.e. the combination is found in a unique record in the members table in a mysql database called test , then members will be redirected to another page (called byimain.php) on my website using.
header('Location: byimain.php');
This works perfectly when I run it on localhost where the mysqli connect function is as follows:
$dbc = @mysqli_connect('localhost', 'root', 'password', test') OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
Now I sign up with yahoo as my web hosting. I have to set up access to mySQL and to create a new username and password for mySQL database (phpMyadmin). I also created a database called test to store my members table, and on my members table, I have one row with member_name = janekoo and pass = passjk . Yahoo told me the host now is mysql instead of localhost. So my new mysqli connect function will be:
$dbc = @mysqli_connect('mysql', 'xxx', 'yyy', test') OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
where xxx =username and yyy = password