I have 3 check-boxes where I can submit them into MySQL. It inserts perfectly, but the problem is, is when I go back to to edit the game, the check-boxes are not checked at all with their correct values (they are empty).
I know there are a lot of answers on this website about this problem, but I found none of them to work. Maybe its something that Im doing wrong with the code. I will post it here.
THIS IS THE SHORT VERSION OF MY CODE, BECAUSE ITS TOO BIG.
if (isset($_GET['add']) || isset($_GET['edit'])) {
$checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? sanitize($_POST['available_consoles']) : '');
if (isset($_GET['edit'])) {
$checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? $_POST['available_consoles'] : $game['available_consoles']);
}
if ($_POST) {
// Separate ech checkbox value with a SPACE into the database.
$checkbox = implode(', ', $_POST['available_consoles']);
// DO INSERT HERE
}
}
<!-- Add the Add Form -->
<form action="games.php?<?php echo ((isset($_GET['edit'])) ? 'edit='.$edit_id : 'add=1'); ?>" method="post" enctype="multipart/form-data">
<!-------------- AVAILABLE CONSOLES ------------------->
<div class="form-group col-md-6">
<label for="checkbox">Available Consoles: </label>
<label class="checkbox">
<input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="Xbox One">
</label>
<label class="checkbox">
<input type="checkbox" <?php if (in_array('PS4', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PS4">
</label>
<label class="checkbox">
<input type="checkbox" <?php if (in_array('PC', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PC">
</label>
</div>
<div class="form-group pull-right">
Cancel
<input type="submit" value="<?php echo ((isset($_GET['edit'])) ? 'Edit ' : 'Add '); ?> Game" class="btn btn-success">
</div>
</form>
Getting these error message next to checkbox:
Notice: Undefined index: available_consoles in C:\xampp\htdocs\Gamesite\admin\games.php on line 425
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\Gamesite\admin\games.php on line 425
name="available_consoles[]" value="Xbox One">
Getting it for all 3 checkboxes
this is what you need
<input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?>id="checkbox" name="available_consoles[]" value="Xbox One">
replace in_array('Xbox One'
with the other consoles names for each input
Here is simple function:
function checked($t,$v){
if (in_array("{$t}", $v)) {
return 'checked';
}
}
use : <?php echo checked('Xbox One',$con); ?>
it was not an array nor was it from $_POST it was actually from the DB so i appended my code to match the answer.
Related
I wanted to create a selection pages (by using checkboxes) where at the end it will show the result from the selection of all the checkboxes. There are about 5 different categories of checkboxes in my first page (only contains HTML) which is for emotions, time, movements, tools & personalization.
First page: index.php
<div>
<form action="process.php" method="post">
<label>Select your emotions :</label>
<input type="checkbox" name="emotions[]" value="Sadness"> Sadness
<input type="checkbox" name="emotions[]" value="Anxiety"> Anxiety
<input type="checkbox" name="emotions[]" value="Anger"> Anger
<input type="checkbox" name="emotions[]" value="Disgust"> Disgust
<input type="checkbox" name="emotions[]" value="Fear"> Fear
<input type="checkbox" name="emotions[]" value="Surprised"> Surprised
<br>
<label>Select your time to commit :</label>
<input type="checkbox" name="time[]" value="1"> Less than 1 minute
<input type="checkbox" name="time[]" value="1-3"> 1-3 minutes
<input type="checkbox" name="time[]" value="3-5"> 3-5 minutes
<input type="checkbox" name="time[]" value="5-10"> 5-10 minutes
<input type="checkbox" name="time[]" value="10-15"> 10-15 minutes
<input type="checkbox" name="time[]" value="15-30"> 15-30 minutes
<br>
<label>Select how you want to move :</label>
<input type="checkbox" name="movement[]" value="Lying Down"> Lying Down
<input type="checkbox" name="movement[]" value="Sitting"> Sitting
<input type="checkbox" name="movement[]" value="Standing"> Standing
<input type="checkbox" name="movement[]" value="Stretching"> Stretching
<input type="checkbox" name="movement[]" value="Walking"> Walking
<input type="checkbox" name="movement[]" value="Work-out"> Work-out
<br>
<label>Tools around me :</label>
<input type="checkbox" name="tools[]" value="Me"> Me
<input type="checkbox" name="tools[]" value="Pen & Paper"> Pen & Paper
<input type="checkbox" name="tools[]" value="Bottle"> Bottle
<input type="checkbox" name="tools[]" value="Color Pencils"> Color Pencils
<input type="checkbox" name="tools[]" value="Drinks"> Drinks
<input type="checkbox" name="tools[]" value="Snacks"> Snacks
<label>Personalization :</label>
<input type="checkbox" name="personalization[]" value="Favorites"> Favorites
<input type="checkbox" name="personalization[]" value="New"> New
<input type="checkbox" name="personalization[]" value="Completed"> Completed
<input type="checkbox" name="personalization[]" value="Islamic"> Islamic
</div>
<div>
<button type="submit">Submit</button>
</div>
2nd page: process.php
<?php
$emotions = (isset($_POST['emotions'])) ? $_POST['emotions'] : array(); ?>
<p><strong>Emotions :</strong>
<?php
if (count($emotions) > 0) {
foreach ($emotions as $emotions) {
echo $emotions . ' ';
}
} else {
echo "No emotions has been selected";
}
?>
</p>
<?
$time = (isset($_POST['time'])) ? $_POST['time'] : array(); ?>
<p><strong>Time :</strong>
<?php
if (count($time) > 0) {
foreach ($time as $time) {
echo $time . ' ';
}
} else {
echo "No time has been selected";
}
?>
</p>
<?
$movement = (isset($_POST['movement'])) ? $_POST['movement'] : array(); ?>
<p><strong>Movement :</strong>
<?php
if (count($movement) > 0) {
foreach ($movement as $movement) {
echo $movement . ' ';
}
} else {
echo "No movements has been selected";
}
?>
</p>
<?
$tools = (isset($_POST['tools'])) ? $_POST['tools'] : array(); ?>
<p><strong>Tools :</strong>
<?php
if (count($tools) > 0) {
foreach ($tools as $tools) {
echo $tools . ' ';
}
} else {
echo "No tools has been selected";
}
?>
</p>
<?
$personalization = (isset($_POST['personalization'])) ? $_POST['personalization'] : array(); ?>
<p><strong>Personalization :</strong>
<?php
if (count($personalization) > 0) {
foreach ($personalization as $personalization) {
echo $personalization . ' ';
}
} else {
echo "No personalization has been selected";
}
?>
</p>
Check if you have entered proper path in action attribute of form tag to reach the "process.php" file
Review your foreach.
What you do is :
$array = [1, 2, 3];
foreach($array as $array){
// $array is no more an array, but have the first value of the init array : 1
}
it's like if you do : $array = $array[0]
you have to do something like :
$array = [1, 2, 3];
foreach($array as $ar){
...
// code for $ar
...
}
I made a search filter using radio buttons and I wanted to store the value from the radio button to a session. The value that will be stored in the session will be used to make the radio buttons stay checked even after reload.
`<script>
function handleRadio(elm)
{
window.location="equipment.php?section=";
<?php
$_SESSION['state']="";?>+elm.value;
}
</script>
<form action="" method="POST">
<?php
$state=$_SESSION['state'];
?>
<input type="radio" name="state" value="allstate" onchange="javascript:handleRadio(this)" <?php echo $state==='allstate' ? 'checked' : '' ?>> All State <br>
<input type="radio" name="state" value="new" onchange="javascript:handleRadio(this)" <?php echo $state==='new' ? 'checked' : '' ?>> New EQ: <br>
<input type="radio" name="state" value="old" onchange="javascript:handleRadio(this)" <?php echo $state==='old' ? 'checked' : '' ?>> Old EQ: <br>
<input type="radio" name="state" value="unknown" onchange="javascript:handleRadio(this)" <?php echo $state==='unknown' ? 'checked' : '' ?>> Unknown State: <br>
</form> `
The function hadleRadio() gets the value from the radio button and passes it to a session. The session will be stored later in a php variable. However, I cant think of any solution to combine PHP and Javascript to store the value from the radio as the current has syntax errors.
I have a page that contains multiple forms that are dynamically created based on the users input, so a page can have just one form or many. The forms consist of two sets of radio buttons, the second set is disabled by default and is enabled based on the users choice in the first set of radio buttons.
This part works absolutely fine, when the user makes the appropriate selection from the first question the second question is made available. However, this applies to all the forms at the same time, so when the selection is made on form 1 all the other forms react based on form 1's input.
Each form has a dynamic class using the id from the database so how do I tell jquery which form the user is interacting with? So that when the user makes a choice on one form it only affects the second set of questions on that particular form?
$(document).ready(function() {
$('.mealSection').prop('disabled', true).css('opacity', '.2');
$('.attendRadio').change(function() {
if ($(this).val() === "Attending") {
$(".mealSection").prop('disabled', false).css('opacity', '1');
} else {
$(".mealSection .mealRadio").prop('checked', false);
$('.mealSection').prop('disabled', true).css('opacity', '.2');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="processRSVP.php" method="post" class="rsvpForm[<? echo $r['GuestID']; ?>]">
<fieldset class="attendSection">
<input type="hidden" value="<? echo $r['GuestID']; ?>" name="id">
<div class="attendOption">
<input class="attendRadio" type="radio" name="attend[<? echo $r['GuestID']; ?>]" value="Attending">Hell Yeah!
</div>
<div class="attendOption">
<input class="attendRadio" type="radio" name="attend[<? echo $r['GuestID']; ?>]" value="Not Attending">...Nah
</div>
</fieldset>
<fieldset class="mealSection">
<div class="mealOption">
<input class="mealRadio" type="radio" name="starter[<? echo $r['GuestID']; ?>]" value="Option 1">Option 1
</div>
<div class="mealOption">
<input class="mealRadio" type="radio" name="starter[<? echo $r['GuestID']; ?>]" value="Option 2">Option 2
</div>
</fieldset>
<div class="rsvpButtonContainer">
<input type="submit" name="<? echo $r['GuestID']; ?>" value="confirm">
</div>
</form>
<form action="processRSVP.php" method="post" class="rsvpForm[<? echo $r['GuestID']; ?>]">
<fieldset class="attendSection">
<input type="hidden" value="<? echo $r['GuestID']; ?>" name="id">
<div class="attendOption">
<input class="attendRadio" type="radio" name="attend[<? echo $r['GuestID']; ?>]" value="Attending">Hell Yeah!
</div>
<div class="attendOption">
<input class="attendRadio" type="radio" name="attend[<? echo $r['GuestID']; ?>]" value="Not Attending">...Nah
</div>
</fieldset>
<fieldset class="mealSection">
<div class="mealOption">
<input class="mealRadio" type="radio" name="starter[<? echo $r['GuestID']; ?>]" value="Option 1">Option 1
</div>
<div class="mealOption">
<input class="mealRadio" type="radio" name="starter[<? echo $r['GuestID']; ?>]" value="Option 2">Option 2
</div>
</fieldset>
<div class="rsvpButtonContainer">
<input type="submit" name="<? echo $r['GuestID']; ?>" value="confirm">
</div>
</form>
I get that at the moment all that my jquery is doing is waiting for a change of state on the first radio buttons and that it doesn't care which form the change comes from. I've tried using focus but my understanding is that only works with the form elements not the actual form itself? I've tried looping through each form but that doesn't seem to work either, although I have a feeling that I'm just going about it all wrong.
Any help at all is greatly appreciated, just a nudge in the right direction would be great.
If your callback takes a event argument you can access the element the user is interacting with with event.target1 and you can access the containing form using the parents2 jQuery method and from there you can call find3 on the form element to specifically access only elements inside of it:
$('.attendRadio').change(function(event) {
var formEl = $(event.target).parents('form');
if ($(this).val() === "Attending") {
formEl.find(".mealSection").prop('disabled', false).css('opacity', '1');
} else {
formEl.find(".mealSection .mealRadio").prop('checked', false);
formEl.find('.mealSection').prop('disabled', true).css('opacity', '.2');
}
});
[1] https://developer.mozilla.org/en-US/docs/Web/API/Event/target
[2] https://api.jquery.com/parents/
[3] https://api.jquery.com/find/
This question already has answers here:
How to set HTML5 required attribute in Javascript?
(6 answers)
Closed 5 years ago.
the required in the script do not working
$("#s_postby").attr('required', false); // put the id of the input field that you whant required
$("#s_postby").attr('required', true); // put the id of the input field that you whant required
I make some radio field in my osclass theme on the item post!
<div id="postby" class="property-ads-100">
<label>
<span class="required_fields">*</span>
<?php _e('Posted By', 'ctg_housing'); ?>
</label>
<div class="item-post-postby-checkbox columns-0">
<input type="radio" name="s_postby" value="owner" id="s_postby0" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='owner') { echo 'checked="checked"'; }; ?>>
<label for="s_postby0">
<?php _e('Owner', 'ctg_housing'); ?>
</label>
</div>
<div class="item-post-postby-checkbox columns-0">
<input type="radio" name="s_postby" value="agent" id="s_postby1" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='agent') { echo 'checked="checked"'; }; ?>>
<label for="s_postby1">
<?php _e('Agent', 'ctg_housing'); ?>
</label>
</div>
<div class="item-post-postby-checkbox columns-0">
<input type="radio" name="s_postby" value="broker" id="s_postby2" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='broker') { echo 'checked="checked"'; }; ?>>
<label for="s_postby2">
<?php _e('Broker', 'ctg_housing'); ?>
</label>
</div>
<div class="item-post-postby-checkbox columns-0">
<input type="radio" name="s_postby" value="agency" id="s_postby3" <?php if(isset($housing['s_postby']) && $housing['s_postby'] =='agency') { echo 'checked="checked"'; }; ?>>
<label for="s_postby3">
<?php _e('Agency', 'ctg_housing'); ?>
</label>
</div>
</div>
I hide this on some categories (the required in the script do not work!!) Why??
<script type="text/javascript">
$('#catId').change(function(){
if( $('#catId').val() == "28" || $('#catId').val() == "29" ||
$('#catId').val() == "30" || $('#catId').val() == "31")
{
$("#postby").hide(); // change Posted By with ID you give to the div
$("#s_postby").attr('required', false); // put the id of the input field that you whant required
}else
{
$("#postby").show(); // change Posted By with ID you give to the div
$("#s_postby").attr('required', true); // put the id of the input field that you whant required
}
});
</script>
What i need now it to make this radio fields required when show and not required wen hide! When show and required i need a error message Posted By: This field is required!
Hope someone can help me!
element.required = true;
How to set HTML5 required attribute in Javascript?
I have used the code below elsewhere in my site and it works. For the life of me I can't understand why it doesn't work here. PROBLEM: keeps throwing alert message.
My HTML:
<form action="processForms.php" method="post" id="joinCommitteeForm" class="headForm shadow">
<div class="outerBlue" style="background:white">
<button type="button" id="cancelForm" class="button" title="Cancel">X</button>
<br><br>
<h3>Get involved—join a committee!</h3>
<?php if(empty($name)||empty($email)||empty($workPhone)){
echo "<p class='red'>All fields must be filled out...</p><br><br>";
}?>
<label for="name" style="width:40px">Name</label><input type="text" name="name" id="name"/><br>
<label for="email" style="width:40px" class="clear">Email</label><input type="text" name="email" id="email"/><br>
<label for="phone" style="width:40px;margin-bottom:20px" class="clear">Phone</label><input type="text" name="dayPhone" id="phone"/>
<hr>
<p class="clear">Please select a committee from the list below</p><br><br>
<select name="comName" id="comName" style="width:215px;margin-left:50px;float:left">
<option value="">Select one...</option>
<?php
$sql = mysql_query("SELECT committeeName FROM committeeName ORDER BY committeeName");
while ($row = mysql_fetch_assoc($sql)){
echo "<option value = '".$row['committeeName']."'>".$row['committeeName']."</option>";
}
echo "</select>";?>
<input type="submit" name="submitCommitteeInterest" class="button orange-button clear" value="Submit" style="margin-top:40px"/>
<div class="clear"></div>
<p class="small white" style="line-height:1em;margin-top:20px">NSGP never sells or gives away your personal information</p>
</div>
</form>
My JS:
$("#joinCommitteeForm").submit(function() {
if ($.trim($("#email").val()) === "" || $.trim($("#name").val()) === "") {
alert('All fields are required');
return false;
}
});
What I suspect the problem is here, is that you are testing if $name or $email is empty, but since you never define them, they will always be empty, per PHP empty() docs: "A variable is considered empty if it does not exist or if its value equals FALSE".
To fix that, you'll need to change your PHP if-statement to:
<?php if(empty($_POST['name'])||empty($_POST['email'])||empty($_POST['dayPhone'])){
echo "<p class='red'>All fields must be filled out...</p><br><br>";
}?>
Also, your POST parameter $workPhone doesn't seem to exist in your form. I've changed it to dayPhone here.
The variable $_POST will contain all parameters in the POST request from the form that's submitted.