I'm a newbie to php and mysql so here i am.. I'm strugglin with something..this is the story..
I have an array in PHP with strings in it.. now i need to make radio buttons with the values being the ones in this array but i dono how to access the contents of the PHP array in HTML so that I can create a radio button for each of the PHP array's members..and then get the name of the radio button that was selected by the user..
Firstly you'll need to make sure the page has the correct file extension, the page name should be something like "myPage.php" (not "myPage.html").
Secondly, and most importantly, comes the actual PHP for printing each array option. You could do something like the following:
PHP Code:
echo("<form method=\"POST\" action=\"answer.php\">");
// This prints the form tag, directing the page to "answer.php" once submited
echo("<fieldset><legend>Array Options</legend>");
// This prints the fieldset for the radio buttons, to indicate that only 1 radio button can be selected within this area
for ($i=1; $i<=count($array); $i++) {
// This loops from 1 to the number of entries in the array, aka the count() part
echo("<input type=\"radio\" name=\"radioOption\" value=\"" . $i . "\" id=\"option" . $i . "\"><label for=\"option" . $i . "\">" . $array[$i] . "</label>")
// This basically sets up a radio button called radioOption, with the value of $i and the id option$i (i.e. option6)
// The label is related to the option id (i.e. option6) and displays $array[$i], basically the array option from the PHP array.
}
echo("<input type=\"submit\" value=\"Submit Option\"></fieldset>");
echo("</form>");
You'll then need to make an answer.php page to view the option that was choose, you can do this by using the $_POST function to get the value of the string "radioOption"
Thank you for ur reply.. I'm a bit confused.. So this first part with the radio option part should go in the php file were i have my array with the strings in it ?
I've been searching the php.net website and found this
PHP Code:
<?php
$line_class=array();
$line_item=array();
for ($i=$rows_number;$i>0;$i--)
{
$line_class[$i]=$_POST['line_class'.$i.''];
$line_item[$i]=$_POST['line_item'.$i.''];
}
?>
Thank you for ur reply.. I'm a bit confused.. So this first part with the radio option part should go in the php file were i have my array with the strings in it ?
That code should go wherever you want to display the radio buttons in your main file.
Quote:
Originally Posted by Newbie2005
I have no idea what they are doing here..
hehe, nps, we all have to start somewhere.
Quote:
Originally Posted by Newbie2005
sorry i'm a complete newbie so myabe i'm not catching on to the syntax error.. thanks for ur help.
That isn't your fault at all. When i first posted the sample, i didn't realise that i had made a mistake on that line, try using the edited version in the post now; it should work.
Thanx it works now.. it was giving me a notice about array offset and it wasnt displaying the first member of the array so i changed this line ( puuting it here in case someone uses this code later):
PHP Code:
for ($i=0; $i<sizeof($table_names); $i++)
Ok now that this is working my next hurdle is to get the value of the radio option from answer.php and use that index in the array to obtain that array element and execute a query in my main page which uses that array element..
so like if radio option value =1 then i use that array element as the destination of a MYSQL query i'm executing..
How can i go about this... Thanks so much for ur help !!!
so like if radio option value =1 then i use that array element as the destination of a MYSQL query i'm executing..
How can i go about this... Thanks so much for ur help !!!
You'll need to query the MySQL database using a few function...
PHP Code:
$dbh = mysql_connect ("localhost", <username-here>, <password-here>) or die ('I cannot connect to the database because: ' . mysql_error());
// This function connects to the database with <username-here> using <password-here>
mysql_select_db ("dbName");
// This function selects which database you want to choose
$result = mysql_query("SELECT * FROM dbName WHERE id='" . $_POST['radioOption'] . "'");
// This is the query that selects which data you want and from where
while($info = mysql_fetch_array($result)) {
// This loops through each record that has the 'right' id
echo($info['id']);
// Prints the id of the current info. Change ['id'] to the field name you want to print
}
mysql_close($dbh)
// Closes the database connection
Bit confusing at first, but you'll soon get used to using MySQL.
Ok i'm a lil confused.. 'radiooption' only gets a value once the submit button has been pressed right but.. after the submit button has been pressed the page goes to answer.php.... but i need to have the mysql query executed using some variables i saved in the main page...
This is my code in the main page, where i'm loading a file, saving it in a destination and then getting the filename i need for my "load data infile query" the radio button selection is supposed to give me the name of the table in the databse where i load the data...
for ($i=0;$i<sizeof($table_names);$i++)
{
echo "$table_names[$i]<br>";
}
//print_r($table_names);
echo("<form method=\"POST\" action=\"answer.php\">");
// This prints the form tag, directing the page to "answer.php" once submited
echo("<fieldset><legend>Array Options</legend>");
// This prints the fieldset for the radio buttons, to indicate that only 1 radio button can be selected within this area
for ($i=0; $i<sizeof($table_names); $i++)
{
// This loops from 1 to the number of entries in the array, aka the count() part
echo("<input type=\"radio\" name=\"radioOption\" value=\"" . $i . "\" id=\"option" . $i . "\"><label for=\"option" . $i . "\">" . $table_names[$i] . "</label>");
// This basically sets up a radio button called radioOption, with the value of $i and the id option$i (i.e. option6)
// The label is related to the option id (i.e. option6) and displays $table_names[$i], basically the array option from the PHP array.
}
echo("<br> <Br> <input type=\"submit\" value=\"Submit Option\"></fieldset>");
echo("</form>");
/******************************
can only get radioption once submitted.. that takes me to answer.php but need it here to build my query with the right table name which is the element in the array indexed by the value of radiooption
*****************/
$test.= $_POST['radioOption'];
$part3 = " LINES TERMINATED BY '\n' (tag_reg)"; //this is the last part of the query
$test.= $part3;
//$sql= "LOAD DATA INFILE 'expected.txt' INTO TABLE registered_tags LINES TERMINATED BY '\n' (tag_reg)";
$result = mysql_query($test);
Ok i'm a lil confused.. 'radiooption' only gets a value once the submit button has been pressed right but.. after the submit button has been pressed the page goes to answer.php.... but i need to have the mysql query executed using some variables i saved in the main page...
This is my code in the main page, where i'm loading a file, saving it in a destination and then getting the filename i need for my "load data infile query" the radio button selection is supposed to give me the name of the table in the databse where i load the data...
Easily solved. In the <form> tag near the radio buttons, change the action preference to your main page, instead of answer.php.
But if goes back to my main page after the submit button is hit then will it remember previous variables (e.g. $test ) that i had stored in the main page as i need them and the value that radioOption index in the array to execute my query..
I don understand completely how I must go about getting that radio button
value on the uploadfile.php page and using it there itself to execute the
query...
Ok lemme start from the beginning and explain it all properly (I hope)
1. User goes to Form_upload.html (attached) page were a text file is uploaded and once the "Upload File" button is hit the page is redirected to
Uploadfile.php.
2. In Uploadfile.php first it checks whether the "Upload File" button
was hit, if it was then it starts of with the else part..
3. In the else part.. it moves the uploaded file to a local destination and
then checks which tables in the database start with 'reg' stores them in
$table_names and then displays radio buttons for them on Uploadfile.php page...
4. A radio button is selected and the Submit button hit..
5. This is where i'm not sure what approach to take.. then should it go to
Uploadfile.php again and execute the query and go to another page
that just informs the user that "File was sucessfully loaded into
database"
Ok i'm not sure how PHP works with that $ BUT after the hitting the
"submit" button when it comes back again to the Uploadfile.php page ..will it remember the variables that were created previously like the filename that was stored and the partly formed query thats stored in string variable $test... coz I need those variables for executing the database query..
OR
would it be a easier/better idea to get the name of the file loaded and the
name of table obtained from the radioOption value into another page and
execute the query there ?? (how do i get these variables across to the new
page)..
Ok I think I've written a long enough story.. phew.. n by now u must be tired
of reading this..
Form_uplaod.html, Uploadfile.php and answer.php are all attached..