PHP and MySQL Radio Button Name = PHP ECHO - php

How can I get the clicked button value when its name is echoed by PHP?
Here's my code:
<form>
<input type='radio' name=<?php echo $i; ?> id='choice1' value=<?php echo $row['choices_a']; ?> checked/>
<?php echo '' .$row['choices_a']. '' ?>
<input type='radio' name=<?php echo $i; ?> id='choice2' value=<?php echo $row['choices_b']; ?> />
<?php echo ''.$row['choices_b'].''?>
<input type='radio' name=<?php echo $i; ?> id='choice3' value=<?php echo $row['choices_c']; ?> />
<?php echo ''.$row['choices_c'].''?> <br />
</form>
<?php
} echo $total;
?>

You can get value of that radio button is just simply like other inputs
$_POST['input_name']
And in your case if i = input_value,
$total = $_POST['input_value'];
echo $total;
You can Also check if that is clicked or isset
if(isset($_POST['input_value'])){
$total = $_POST['input_value'];
echo $total;
}
Hope it clears.

Related

How can I display the value that I got from input text field for the following codes?

Hye! can anyone help. I'm stuck already.
I want to display the value that I got from the text field for the following code. Oh!!! the $str in the code actually is the value that I got from the dropdown option.
So actually I'm trying to have a dropdown button on the page which is when the user select a value at first dropdown, another dropdown menu / text box will display. As the codes below, different value may have different display for the next display either dropdown OR text box. So now i'm trying to get the value from the next displayed dropdown OR text box. so for now i'm trying to get the value from the text box that displayed after selection from the first dropdown.
Hope this will be clear :)
Thanks for helping
<form name="nameOfForm" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" action="search_handler">
<div class="row">
<div class="col-md-4">
<?php
echo "<b>Search By : </b><br>";
echo "<select class='inputStyle' name='option' onchange='this.form.submit();'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='RefNo'>Reference No</option>";
echo "<option value='Date'>Date</option>";
echo "<option value='Requestor'>Requestor</option>";
echo "<option value='Type'>Type</option>";
echo "</select>";
?>
</div>
<div class="col-md-4">
<?php
$str='';
$maxDate = date("d-m-Y");//todays date
//echo $maxDate;
if($_GET){
//echo $_GET['option'];
$str = $_GET['option'];
//echo $str;
}
if ($str != ''){
if ($str == "RefNo"){
//echo "Hello!";
echo "<b>Type Reference No. Here :</b><br>";
echo "<input class='inputStyle' id='RefNo' name='RefNo' type='text' autocomplete='off' required>";
}else if ($str == "Requestor"){
//echo "Hello!";
echo "<b>Type Requestor Name Here :</b><br>";
echo "<input class='inputStyle' id='ReqName' name='ReqName' type='text' autocomplete='off' required>";
}else if($str == "Type"){
echo "<b>Choose Type :</b><br>";
echo "<select class='inputStyle' name='type'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='PETTY CASH'>Petty Cash</option>";
echo "<option value='OTHERS'>Others</option>";
echo "</select>";
}else{
//echo "Date Here!";
echo "<b>From</b><br>";
echo "<input class='inputStyle' onchange='allowToDate()' id='from' name='from' type='date' max='<?php echo $maxDate; ?>' required>";
echo "<b>To</b><br>";
echo "<input class='inputStyle' id='to' name='to' type='date' max='<?php echo $maxDate; ?>' required disabled>";
}
}
else {
echo "</br>";
echo "Please Select A Value From Dropdown!";
}
?>
</div>
<div class="col-md-2">
<br>
<input class="submitBtnBS" type="button" onclick="search()" id="proceed" name="proceed" value="PROCEED" style="height:35px; width:100%;">
</div>
<div class="col-md-2">
<br>
<input class="clearBtnBS" type="button" onClick="clearForm(this.form)" id="clear" name="clear" value="CLEAR" style="height:35px; width:100%;">
</div>
</div>
</form>
In your HTML File:
<form action="yourphpfile.php" method="get">
<select name="RefNo" id="RefNo" required class="">
<option value="">Choose one</option>
<option>List 1</option>
<option>List 2</option>
<option>List 3</option>
<option>List 4</option>
</select>
<input type='submit' value='submit' name='submit' />
</form>
In your PHP:
<?php
//To prevent having "Undefined index" warnings, you need to check if GET is the request method
if ($_SERVER['REQUEST_METHOD'] == "GET"){
$str = $_GET['RefNo'];
..
..
..
..
..
}
?>
You should create a form (with post or get method). Then when the form is send to your page you can get the inputed value.
Your code modified:
<form name="nameOfForm" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" action="search_handler">
<div class="row">
<div class="col-md-4">
<?php
echo "<b>Search By : </b><br>";
echo "<select class='inputStyle' name='option' onchange='this.form.submit();'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='RefNo'>Reference No</option>";
echo "<option value='Date'>Date</option>";
echo "<option value='Requestor'>Requestor</option>";
echo "<option value='Type'>Type</option>";
echo "</select>";
?>
</div>
<div class="col-md-4">
<?php
$str='';
$maxDate = date("d-m-Y");//todays date
//echo $maxDate;
if($_GET){
//echo $_GET['option'];
$str = $_GET['option'];
//echo $str;
}
if ($str != ''){
if ($str == "RefNo"){
//echo "Hello!";
echo "<b>Type Reference No. Here :</b><br>";
echo "<input class='inputStyle' id='RefNo' name='RefNo' type='text' autocomplete='off' required>";
}else if ($str == "Requestor"){
//echo "Hello!";
echo "<b>Type Requestor Name Here :</b><br>";
echo "<input class='inputStyle' id='ReqName' name='ReqName' type='text' autocomplete='off' required>";
}else if($str == "Type"){
echo "<b>Choose Type :</b><br>";
echo "<select class='inputStyle' name='type'>";
echo "<option value=''>--Please Select--</option>";
echo "<option value='PETTY CASH'>Petty Cash</option>";
echo "<option value='OTHERS'>Others</option>";
echo "</select>";
}else{
//echo "Date Here!";
echo "<b>From</b><br>";
echo "<input class='inputStyle' onchange='allowToDate()' id='from' name='from' type='date' max='<?php echo $maxDate; ?>' required>";
echo "<b>To</b><br>";
echo "<input class='inputStyle' id='to' name='to' type='date' max='<?php echo $maxDate; ?>' required disabled>";
}
}
else {
echo "</br>";
echo "Please Select A Value From Dropdown!";
}
?>
</div>
<div class="col-md-2">
<br>
<input class="submitBtnBS" type="button" onclick="search()" id="proceed" name="proceed" value="PROCEED" style="height:35px; width:100%;">
</div>
<div class="col-md-2">
<br>
<input class="clearBtnBS" type="button" onClick="clearForm(this.form)" id="clear" name="clear" value="CLEAR" style="height:35px; width:100%;">
</div>
</div>
</form>
<?php
if(isset($_GET["RefNo"])){
echo $_GET["RefNo"];
}
if(isset($_GET["ReqName"])){
echo $_GET["ReqName"];
}
?>

saving the options selected in quiz and checking

I have generated a quiz with questions on the same page.I want to save the answers to check with the answers in the database .How can I do it ? Below is the code for how I generated quiz.How to store the selected answer and check?
<?php for($i=1;$i<=3;$i++)//loop for extracting question and options
{?>
<form action = "CHECK.php" method="POST">
<?PHP
$sql = "SELECT QUESTION FROM T_QUESTIONS WHERE QUES_NO=$i LIMIT 1";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>QUESTION $i :</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['QUESTION'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
?>
<?PHP
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {
?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION1']; ?>" /> A. <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION2']; ?>" /> B. <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION3']; ?>" /> C. <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION4']; ?>" /> D. <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION5']; ?>" /> E. <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
</form>
<button style ="color:red;border radius:10px;margin:100px;height:100px;width:200px;font-size:30px"> <a href=CHECK.php>SUBMIT TEST</a></button>
Correct_OPTION is in the same table that of question and options.

passing while loop value to another form

In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">
I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...

PHP foreach array into option value

Without needing to give too much info on the API in use, I'm trying to get the option value to match what's being wrapped inside the option tag.
Something like this:
<option value="Foo">Bar</option>
Here's the PHP:
<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local', array(
"AreaCode" => $_POST["areacode"]));
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option value=''>";
echo $number->phone_number;
$counter++;
echo "</option>";
echo "<br>";
}
echo "</select>";
echo "<br>";
echo $counter;
?>
With this form:
<form action="index.php" method="post">
Area Code:<br>
<input type="text" name="areacode" value=""><br>
<input type="submit" value="Submit">
</form>
Your syntax is incorrect.
echo '<select>';
foreach($numbers->available_phone_numbers as $number) {
echo '<option value="' .$number->phone_number .'">';
echo $number->phone_number;
echo '</option>';
}
echo '</select>';
Also, you shouldn't have a br tag in your select element.
Change echo "<option value=''>"; to echo "<option value='{$number->phone_number}'>";
I think you need to move the select inside the form tag in order to submit the selected data also.
<form action="index.php" method="post">
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option value='".$number->phone_number."'>";
echo $number->phone_number;
$counter++;
echo "</option>";
echo "<br>";
}
echo "</select>";
echo "<br>";
echo $counter;
?>Area Code:<br>
<input type="text" name="areacode" value=""><br>
<input type="submit" value="Submit">
</form>
Hope this help
<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local',
array(
"AreaCode" => $_POST["areacode"]
));
?>
<form action="index.php" method="post">
<?php
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option>" . $number->phone_number . "</option>";
$counter++;
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form>

PHP. Building a drop down menu . Passing around object array is sloppy how do I simplify this

I'm trying to grasp OOP and I decided to build a site that accesses a sql database.
It's been working out great so far I have several tables, but I've run into a snag
I access the database and create an object array with
$Dog_array = $sth->fetchAll(PDO::FETCH_CLASS, 'Dog');
Simplified but my class looks like this.
class Dog {
private $name;
function get_name {
return $this->name;
}
list that build my table
function edit_table($Dog) {
echo "<table><tr><form action='update.php' method ='post'>";
echo "<input type='hidden' name='id' value='" . $id ."'>";
echo "<td>". $Dog->get_id() ."</td>";
?>
<td><input type='text' name='rname' value="<?php echo $Dog->get_rname(); ?>"></td>
<td><input type='text' name='cname' value="<?php echo $Dog->get_cname(); ?>"></td>
<td><input type='text' name='dob' value="<?php echo $Dog->get_dob(); ?>"></td>
<td><input type='radio' name='gender' value='male' <?PHP if($Dog->get_gender() == 'male'){ echo "checked=\"checked\""; } ?> /> Male
<input type='radio' name='gender' value='female' <?PHP if($Dog->get_gender() == 'female'){ echo "checked=\"checked\""; } ?> />Female</td>
<td><input type='text' name='sire' value="<?PHP echo "builddropdown" ?> "></td>
<td><input type='text' name='dam' value="<?PHP drop_menu() ?>"></td>
<?php
if ($Dog->get_available()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
echo "<td><input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/></td>";
if ($Dog->get_display()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
}
echo "<td><input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/></td></tr></table>";
?>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<?php
}
?>
<input type='submit' value='update'/></form>
<FORM METHOD="LINK" ACTION="index.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>
<?php
}
?>
My drop down
function drop_menu() {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?></option>
<?php } ?>
</select>
</form>
<? }
My drop down menu creates a list of all of the dogs.
I get the error Invalid argument supplied for foreach
I get why I get the error I just don't know how to clean this up so I don't have to pass my Object array into my edit_table function so I can pass it into drop down function
.
I thought about creating a child class but the menu is built from an array of the objects not one object so I don't know how to make this work either. New to forum posting, so feel free to recommend some corrections in how I ask for help.
It's a problem with the arrage $Dog_array scope.
You need to pass the $Dog_array to the drop_menu function.
<?php
function drop_menu($Dog_array) {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?>></option>
<?php } ?>
</select>
</form>
<? }
And whenever you need to call the function, pass the array normally.
Also you can pass by reference
<?php
function drop_menu(&$Dog_array) {?>
In this case any manipulation to the array inside the function will impact the original array directly.
function dropdown( $name, array $options, $selected=null )
{
//array_push($options, "");
/*** begin the select ***/
//<select name="dropdownarray" id="dropdownarray">
$dropdown = '<select name="'.$name.'" id="'.$name.'">';
$selected = $selected;
/*** loop over the options ***/
echo "<b>$name : </b>"; // Title
$dropdown .= '<option value=></option>'."\n"; //empty data
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
enter code here

Categories