echo from the submission form value with different IDs - php

I have a submission form with three of them
two of them are okay which I use one ID for all the value that going to be submit
but one of them which I'm struggling now is the one with all different values of course and the selection form also with different ID which made me confuse how can I echo it.
<form action="" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>เลือกช่อง</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="ch41" checked="checked" />
<label for="radio-choice-1">number 1</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="ch80" />
<label for="radio-choice-2">number 2</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="ch44" />
<label for="radio-choice-3>"number 3</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="ch42" />
<label for="radio-choice-4">number 4</label>
<input type="radio" name="radio-choice-1" id="radio-choice-5" value="ch83" />
<label for="radio-choice-5">number 5</label>
</fieldset>
</div>
</form>
and here is what I'm trying to echo
<video width="5%" height="5%" src="http://mydomain:<?php echo $_POST["radio-choice-1"]; ?>-<?php echo $_POST["date"]; ?>-<?php echo $_POST["time"]; ?>" alt="" controls="" tabindex="0">
so the problem is on the "radio-choice-#"
since it has 5 numbers on the ID
here is the example
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="ch80" />
<label for="radio-choice-2">number 2</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="ch44" />
<label for="radio-choice-3>"number 3</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="ch42" />
<label for="radio-choice-4">number 4</label>
<input type="radio" name="radio-choice-1" id="radio-choice-5" value="ch83" />
<label for="radio-choice-5">number 5</label>
so if they choose menu number 5
I want to echo here
<video width="5%" height="5%" src="http://mydomain:<?php echo $_POST["the value of number 5"]; ?">

Your code for actually getting the value from the radio button was already correct. Here is something you could use to keep data more organized and be able to get the 'page' number you're on.
<?php
$items = array('ch41', 'ch80', 'ch44', 'ch42', 'ch83');
$page = array_search((empty($_POST['choice']) ? 'ch41' : $_POST['choice']), $items) + 1; // Change 'ch41' to whatever the default page is
// To name the labels, you can add keys for your values like so:
$items = array('home' => 'ch41', 'page' => 'ch80', 'channel' => 'ch44', 'news' => 'ch42', 'contact' => 'ch83');
$page = (empty($_POST['choice']) ? 'home' : array_search($_POST['choice'], $items)); // Replace 'home' with whatever the default page is
// Then replace the <?= $key + 1 ?> below with just <?= $key ?>
?>
You are currently on page <?= $page ?>.
<form action="" method="post">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>เลือกช่อง</legend>
<?php foreach ($items as $key => $value): ?>
<input type="radio" name="choice" id="radio-choice-<?= $key ?>" value="<?= $value ?>"<?= $key === $_POST['choice'] ? ' checked="checked"' : '' ?> />
<label for="radio-choice-<?= $key ?>">number <?= $key + 1 ?></label>
<?php endforeach; ?>
</fieldset>
</div>
</form>
A rough example. You can add additional channels to the $items array. It will default to page 1 and automatically check the radio button for the page you're currently viewing. I hope this is what you wanted.

Related

Inserting multiple selections of the users in database with PHP through checkbox input type

I am developing a website which has a form that enable a user to choose multiple choices through input type "Checkbox", I have stacked on how to insert multiple selections of the user in the database..
Here is my code:
if(isset($_GET['add']) || isset($_GET['edit'])){
$fullname = ((isset($_POST['fullname']))? clean($_POST['fullname']):'');
$win = ((isset($_POST['win']))? clean($_POST['win']):'');
$nominated = ((isset($_POST['nominated']))? clean($_POST['nominated']):'');
$bio = ((isset($_POST['bio']))? clean($_POST['bio']):'');
$movies = ((isset($_POST['movies']))? clean($_POST['movies']):'');
$birth = ((isset($_POST['birth']))? clean($_POST['birth']):'');
$insertSQL="INSERT INTO crew (fullname,win,nominated,image,movies,bio,birth)
VALUES('$fullname','$win','$nominated','$dbpath','$movies','$bio','$birth')";
$_SESSION['success']= 'Crew Member Added successfully';
header('Location: crew.php');
$db->query($insertSQL);
<div class="form-group col-md-3">
<label for="movies">MOVIES YOU CONTRIBUTED IN:</label><br>
<?php while ($movies = mysqli_fetch_assoc($sql)) { ?>
<input type="checkbox" name="movies" value="<?=$movies['title'];?>"><?=$movies['title'];?><br>
<?php } ?>
</div>
<div class="form-group col-md-6">
<label for="awards">Awards Winning:</label>
<?php while ($winning = mysqli_fetch_assoc($sql1)) { ?>
<input type="checkbox" name="win" value="<?=$winning['name'];?>"><?=$winning['name'];?><br>
<?php
} ?>
</div>
<div class="form-group col-md-6">
<label for="awards">Awards Nominated:</label>
<?php while ($nom = mysqli_fetch_assoc($sql2)) { ?>
<input type="checkbox" name="nominated" value="<?=$nom['name'];?>"><?=$nom['name'];?><br>
<?php } ?>
</div>
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

filter price by radio button in php mysql

i am working on e-commerce website and in products section i want to give a function to sort product by price range like 100 to 500 but i have no idea to connect html radio button with mysql that on click product get sort on screen.
[enter image description here][1]
This is html code for radio button
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<ul class="radio">
<li><input type="radio" name="radio" id="r1" value="radio"/>
<label for="radio1">100-500</label></li>
<li><input type="radio" name="radio" id="r2" value="radio2"/>
<label for="radio2">600-1000</label></li>
<li><input type="radio" name="radio" id="r3" value="radio"/>
<label for="radio3">1100-1500</label></li>
<li><input type="radio" name="radio" id="r4" value="radio4"/>
<label for="radio4">1600-2000</label></li>
<li><input type="radio" name="radio" id="r5" value="radio5"/>
<label for="radio5">2100-2500</label></li>
</ul>
</form>
And this php and mysql code
<?php
$results1 = mysqli_query($db,"SELECT product_code, product_name, product_img_name, price FROM products where price> 500 ORDER BY id ASC");
if($results1){
$products_item = '<ul class="products">';
while($obj = $results1->fetch_object())
{
$products_item .= <<<EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content"><h3>{$obj->product_name}</h3>
<div class="product-thumb"><img height="250px" src="../../img/{$obj->product_img_name}"></div>
<div class="product-info">
Price {$currency}{$obj->price}
<fieldset>
<label>
<span>size</span>
<select name="product_color">
<option value="Black">Medium</option>
<option value="Silver">Small</option>
</select>
</label>
<label>
<span>Quantity</span>
<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</label>
</fieldset>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<div align="center"><button type="submit" class="add_to_cart">Add</button></div>
</div></div>
</form>
</li>
EOT;
}
}
?>

Can I check a checkbox outside of an HTML input tag?

I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>

how to stop response time and count mouse clicks

The code below is a dynamic way of displaying each option as checkbox buttons for each question:
function ExpandOptionType($option) {
$options = explode('-', $option);
if(count($options) > 1) {
$start = array_shift($options);
$end = array_shift($options);
do {
$options[] = $start;
}while(++$start <= $end);
}
else{
$options = explode(' or ', $option);
}
echo '<p>';
foreach($options as $indivOption) {
echo '<div id="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
echo '</p>';
}
foreach ($arrQuestionId as $key=>$question) {
?>
<p><?php echo ExpandOptionType(htmlspecialchars($arrOptionType[$key])); ?></p>
<p><input type='text' class='questionIds' name='questionids' value='<?php echo htmlspecialchars($arrQuestionId[$key]); ?>' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
}
Now below I 2 text inputs which are also in the foreach loop, one for response time and other is for counting mouse click:
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
Now this is my questions:
The response time text input contains a count up timer. What I want is that if the first button checkbox is clicked in a question, the question's response timer should stop. This is so we know how long it took the user to respond answering a particular question
The mouse click text starts with 0 and what I want this text input to do is for every button checkbox that is clicked in a question, the question's mouse click text input counts up the amount of clicks so we know how many clicks on the question's options the user has compiled.
How can the above be achieved?
Below is a jsfiddle showing sample code of what it looks like for one question:
http://jsfiddle.net/zAFND/630/
UPDATE:
Source code showing multiple questions example:
QUESTION 1:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-F" value="F" />
<span>F</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='73' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
QUESTION 2:
<p>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-A" value="A" />
<span>A</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-B" value="B" />
<span>B</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-C" value="C" />
<span>C</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-D" value="D" />
<span>D</span>
</label>
</div>
<div id="ck-button">
<label class="fixedLabelCheckbox">
<input type="checkbox" name="options[]" id="option-E" value="E" />
<span>E</span>
</label>
</div>
</p>
<p><input type='text' class='questionIds' name='questionids' value='74' /></p>
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
<p><input type='text' class='mouseClick' name='mouseclick' value='0' /></p>
As you created a reference for setinterval that is good. Now you can remove the set interval on click event of first checkbox and add a click event on all checkbox to increase counter.
That will be
$(document).ready(function(){
var checkBox=$('#ck-button').find('input');
var responsetimer=//your interval function
checkbox.filter(':first').bind('click',function(e){
clearInterval(responsetimer);
});
checkbox.bind('click',function(e){
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
});
Well i am not sure about the question you want timer to be stopped at first button click or first time button clicked. If it is first time than dont use the first binding . keep clear interval in second binding itself.
checkbox.bind('click',function(e){
clearInterval(responsetimer);
$('.mouseClick').val(parseInt($('.mouseClick').val())+1);
});
JS fiddle :http://jsfiddle.net/zAFND/631/
for second option
http://jsfiddle.net/zAFND/638/
UPDATE
if you want this for multiple question wrap each question with a div say <div class="queWrap"></div>
Make a array refrence for your interval function so that it can be clear.
Loop to each queWrap and start timer and assign events on checkbox.
Check example for multiple question :http://jsfiddle.net/zAFND/640/

Radio Buttons & Sessions, how to get value and use as a session variable

How would i get the value of a radio button and use that in my session so for example;
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
</div>
as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?
#Xavier: Try --
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
</div>

Categories