I have form page calling a function that pulls 2 random names from a mysql database (called by php function pairsim()). I want to create a group of checkboxes that will create conditions that I can use to limit to mysql pulls. These conditions then need to live on until the user changes the configurations (update button below).
I am hoping to create an array of checked values to use as a condition until the user updates his configurations again. Unfortunately, I can't get the array to persist while going page to page. I am also having trouble creating an array to pass back into the pairsim(). Forgive my ignorance, is there anyway to accomplish what I am trying to do?
select_checkbox.php:
<?php
session_start();
$_SESSION['G'] = isset($_POST['pG']) ? 1 : 0 ;
$_SESSION['D'] = isset($_POST['pD']) ? 1 : 0 ;
$_SESSION['W'] = isset($_POST['pW']) ? 1 : 0 ;
$_SESSION['C'] = isset($_POST['pC']) ? 1 : 0 ;
?>
<button class="btn btn-success" type="button" data-toggle="collapse" data- target="#collapselimit" aria-expanded="false" aria-controls="collapselimit">
Limit Names
</button>
<div class="collapse container-fluid" id="collapselimit">
<form role="form" method="post" action=<?php pairsim() ; ?>>
<div class="panel panel-default">
<div class="panel-body">
<p><b>Position Limited:</b><br></p>
<!--create array with checked values to include to mysql function-->
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pG' checked>Goalies</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pD' checked>Defensemen</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pW' checked>Wingers</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pC' checked>Centers</label>
<div>
<!--locks in checkbox configuratons-->
<input type="submit" class="btn btn-success btn-lg" value="Update"/>
<!--script below to check/uncheck all-->
<input type="button" class="btn btn-default btn-lg" id="CheckAll" value="Check All" />
<input type="button" class="btn btn-default btn-lg" id="UncheckAll" value="Clear All" />
</div>
</form>
</div>
</div>
I see a few errors here, I'll try to go in procedural order.
Your checkboxes all use the same name, so the resulting post var would be $_POST['positionsel'];. So your existing session check is always going to be false.
BTW the output var_dump($_POST['positionsel']); with everything checked would look like:
array(4) {
[0]=>
string(2) "pG"
[1]=>
string(2) "pD"
[2]=>
string(2) "pW"
[3]=>
string(2) "pC"
}
Fix:
session_start();
$checkBoxValues = array('pG', 'pD', 'pW', 'pC');
foreach($checkBoxValues as $check)
{
if(in_array($check, $_POST['positionsel']))
{
$_SESSION[$check] = true;
} else {
$_SESSION[$check] = false;
}
}
This will check the $_POST variable for the checkbox values, if it exists that means it was checked.
What this will then cause is a situation where if you do not submit a form, then no post value will process, causing the $_SESSION variables to be wiped. TO prevent this, we need to check if the form was in fact submitted. A cheap way to do that is to name the submit field:
<input name="submitted" type="submit" class="btn btn-success btn-lg" value="Update"/>
And before the foreach, do check for it:
if(isset($_POST['submitted']))
{
$checkBoxValues = array('pG', 'pD', 'pW', 'pC');
foreach($checkBoxValues as $check)
{
if(in_array($check, $_POST['positionsel']))
{
$_SESSION[$check] = true;
} else {
$_SESSION[$check] = false;
}
}
}
Now the session vars will only be overwritten if the form is actually submitted.
Next, you need to change your check boxes to only be "checked" if they were in fact checked:
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pG' <?PHP if($_SESSION['pG']) { echo "checked"; }?> >Goalies</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pD' <?PHP if($_SESSION['pD']) { echo "checked"; }?> >Defensemen</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pW' <?PHP if($_SESSION['pW']) { echo "checked"; }?> >Wingers</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pC' <?PHP if($_SESSION['pC']) { echo "checked"; }?> >Centers</label>
That should then allow the form to reload with the already configured checkboxes, preserving your preferences across resubmits & new page loads as long as the session stays alive.
In this example I changed your session variable names to the check box values, so keep that in mind if you paste it over as you'll need to adjust it anywhere else you use the session (or adjust my code to use your original varnames).
Related
As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).
This is my code:
<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >
<?php
if (mysqli_num_rows($result) > 0)
{ ?>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
<div class="card-body">
<input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</div>
</form>
And this is the process
<?php
if(ISSET($_POST['mo'])){
$demandeur = $_SESSION['username'];
$model = $_POST['model'];
$chassit = $_POST['chassit'];
$sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
VALUES ('$demandeur','$model','$chassit')";
if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
?>
The input name="model" it insert always the last value !!!
Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:
<label class="card-body">
<input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</label>
Please note:
you never use $_POST['mo'] value that should contain selected model
You have same ID in every iteration, and that is invalid HTML
Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.
Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.
There are a few ways I can think of to make this work:
Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.
I am trying to make a random tournament generator, where I can select names from a list with checkboxes and then randominze them into a different order.
I have the following form:
<form method="post" action="<?php echo ROOT ?>HomeController/createTournament/" enctype="multipart/form-data">
<div class="form-group">
<label for="participants">Select participants</label><br>
<?php foreach($players as $p): ?>
<input type="checkbox" name="participants" value="<?php echo $p['name'];?>"> <?php echo $p['name'];?><br>
<?php endforeach; ?>
</div>
<button type="submit" class="btn btn-primary btn-block" name="create">Show participants</button>
</form>
This form show's a checkbox and behind the checkbox the name of the participant.
This is my method:
public function createTournament() {
if(isset($_POST["create"])) {
$participants = $_POST['participants'];
}
include('app/views/showTournament.php');
}
That means I am saving the checked ones into $participants, right?
In the file showTournament, I know have access to $partipants.
I try to var_dump $particpants and it shows me:
string(6) "Onlyoneselected name"
So I tried a foreach, to get ALL of the selected names.
<?php
foreach($participants as $p) {
echo $p;
}
;?>
The foreach isn't showing anything, but the file has access to $participants. I want all the names on my screen, so I can start randomizing them. What do I do wrong?
<input type="checkbox" name="participants"
This line here is the root of your problems.
Because every checkbox has the same name, the value of $_POST['participants'] gets overridden for each checkbox in the list.
If you change that snippet to:
<input type="checkbox" name="participants[]"
Then $_POST['participants'] becomes an array of all checked values.
You need multiple checkbox values.
And therefore, HTML name of the input should be multiple (array)
<input type="checkbox" name="participants" will return string, only latest submitted value.
<input type="checkbox" name="participants[]" will return array of all submitted values.
So, replacing name="participants" to name="participants[]" will work.
I already connected my sql to database, but my isset post does not work
<div class="container" style="margin-top:60px;margin-bottom:60px;">
<div class="form-group pull-right">
</div>
<div class="row">
<p> Set Start DateTime<p>
<input type="datetime-local" name="start"><br><br>
<button type="submit" class="btn btn-primary" name="submit1">SUBMIT</button>
<br><br>
<p> Set Deadline<p>
<input type="datetime-local" name="end"><br><br>
<button type="submit" class="btn btn-primary" name="submit2">SUBMIT</button>
<br><br>
</div>
</div>
<?php
if(isset($_POST['submit1'])){
$name = $_POST['start'];
$sql = "UPDATE tbdeadline SET
start = '$name'
WHERE id = '1'
";
if ($this->con->query($sql) === TRUE){
echo '<script>window.location.href="deadline.php"</script>';
}
else{
echo 'error';
}
}
?>
I want to update datetime in my "user" database whose table name is "start" and id = "1"
But my html is getting nowhere. It is not showing any error or anything else.
What am I doing wrong, how can I fix this?
I think value from is not send via $_POST. If I want to check if form is send I use
<input type="hidden" name="submit1" value="1" />
And check via
if (isset($_POST['submit1'])
Try it.
The sql code is vulnerable to SQL injection - or it would be if it were not within single quotes rather than double. Using single quotes means that any PHP variable within must be escaped - easier to use double quotes and the values of the variables will be OK.
<p> Set Start DateTime<p>
<input type='datetime-local' name='start'><br><br>
<button type='submit' class='btn btn-primary'>SUBMIT</button>
<br><br>
<p> Set Deadline<p>
<input type='datetime-local' name='end'><br><br>
<button type='submit' class='btn btn-primary'>SUBMIT</button>
<br><br>
</div>
</div>
<?php
if( isset( $_POST['start'],$_POST['end'] )){
$name = $_POST['start'];
$end = $_POST['end'];
/* This is vulnerable to SQL injection !! */
/* use double quotes around sql or better mysqli/PDO and `prepared statements` */
$sql = "UPDATE tbdeadline SET start = '$name' WHERE id = '1'";
if( $this->con->query($sql) === TRUE ){
echo '<script>window.location.href='deadline.php'</script>';
} else{
echo 'error';
}
}
?>
You forgot to add the values for your Submit buttons.
You need to update your Submit buttons like below:
<button type="submit" class="btn btn-primary" name="submit1" value="start">SUBMIT</button>
<button type="submit" class="btn btn-primary" name="submit2" value="end">SUBMIT</button>
Also make sure your form method must be POST like below:
<form method="post">
If you still get isset error then you need to debug your $_POST to check whether form data is coming via form or not. Add below code before if condition to test:
print_r($_POST); die;
And use MySqli Prepared Statements for updating your query to make it more Secure.
Follow below link to learn more:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
I have problem with change images size in posts (after click on the buttons grid/list).
these are the buttons that should (after click) change $value to true or false (that scale images):
<div class="products_look">
<input type="button" class="view_list" onclick="" value="List" />
<input type="button" class="view_grid" onclick="" value="Grid" />
</div>
and this is the php functions that generated grid or list view in the posts:
<?php
if($value == true) {
echo image_products_grid();
}
else {
echo image_products_list();
}
?>
What I need to insert in onClick tags that after clicking on the button returned php $value (true or false)?
Why not use a parameter in the URL? Something like this:
<div class="products_look">
<input type="button" class="view_list" onclick="window.location = 'display.php?type=LIST'" value="List" />
<input type="button" class="view_grid" onclick="window.location = 'display.php?type=GRID'" value="Grid" />
</div>
And then on your PHP page:
<?php
if($_GET["type"] == "GRID") {
echo image_products_grid();
}
else {
echo image_products_list();
}
?>
Is this the type of solution that you are looking for? It's not terribly elegant, but your question is a little unclear so I hope this helps.
I have this PHP code:
$result = mysql_query("SELECT distinct om_quote_no from `porders` order by om_quote_no desc") or die(mysql_error());
echo '<select name="project_no">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['om_quote_no'].">".$row['om_quote_no']."</option>";
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
<div id="table">
<?php
if($_GET){
So as you can see the PHP is forming a dropdown input and once submitted its going to execute some code, but what i need to do is have two dropdown inputs and therefore two submit buttons, however i'm not sure how to form the PHP if statement to distinguish which submit was pressed, so i'll have(pseudo):
if (submit1){
}
if (submit2){
}
Is that possible?
If you give your <input type="submit"> elements names, the one that is clicked will have its name and value sent to the server.
<input type="submit" value="Submit" name="submit1">
if clicked will send submit1=Submit to the server. You could therefore check with if ($_GET['submit1']) to see if it was pressed.
This isn't the best way but can do something like this too:
<select name="test" onchange="document.location ='test.php?submit=dropdown1'">
<option>test</option>
<option>test1</option>
</select>
<br><br>
<select name="test1" onchange="document.location ='test.php?submit=dropdown2'">
<option>test2</option>
<option>test3</option>
</select>
within test.php file:
if($_GET['submit'] == 'dropdown1')
{
print "One";
//statements to execute
}elseif($_GET['submit'] == 'dropdown2'){
//statements here to execute
print "Two";
}
<input type="submit" value="Submit 1" name="submit1"/>
<input type="submit" value="Submit 2" name="submit2"/>
<?php
if(isset($_POST['submit1'])){
//do stuff
//grab select option
$select_option=$_POST['project_no'];
}else if(isset($_POST['submit2'])){
//do stuff
}else{
//do stuff
}
?>
Or if your method is GET than change $_POST to $_GET :)