Checked box should stay checked PHP - php

if i check the checkbox, the checkbox should stay checked. I got a form where it gets checked after submit, but it should staying checked without any Submit. is it possible with php?
Code:
$checkbox=false;
if (isset($_POST['psp-ele'])){
$checkbox=true;
};
?>
<input type="checkbox" name="psp-ele" id="Investnr" class="Investnr" <?php if ($checkbox==true) { echo "checked='checked'";}; ?> >

If you want the checkbox to stay checked when you load the page without a form submit then you should keep that information in the session and set the value from the session when parsing the checkbox:
In your form processor:
session_start();
$_SESSION['psp-ele'] = isset($_POST['psp-ele']);
Where ever you want to parse it:
<?php
// start the session if it has not already been started
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
?>
<input type="checkbox" name="psp-ele" id="Investnr" class="Investnr" <?php if ($_SESSION['psp-ele']) { echo 'checked'; }; ?> >

The initial load of your page that contains the form is likely to happen via a GET request - so simply add a check if the request method was GET, and also set your flag to true in that case:
$checkbox=false;
if ($_SERVER['REQUEST_METHOD'] == 'GET' || isset($_POST['psp-ele'])){
$checkbox=true;
};

If you want the form to be checked by default and after submit want to persist the users selection then you can simply add another condition which checks if the variable has been defined or not with isset.
if(!empty($_POST)){
if (isset($_POST['psp-ele'])){
$checkbox = true;
}else{
$checkbox = false;
}
}
<input type="checkbox" name="psp-ele" id="Investnr" class="Investnr" <?php if (!isset($checkbox) || $checkbox) { echo "checked='checked'";}; ?> >
The isset checks the checkbox when loading for the first time and the $checkbox variable does it after being submitted.

Related

post isset($_POST['next'] is not set after submit /refresh page. Need to be press twice for it to be set how to fix?

<script>
function refreshPage()
{
document.forms[0].submit();
}
</script>
<?php
foreach($values as $value)
{
?>
<input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>"
<?php if(isset($_POST['food'])) echo "checked='checked'"; ?> > // note this is still part of
input
<?php
}
?>
<button name = "pass" onClick = "refreshPage()">refresh</button>
<?php
if(isset($_POST['pass'])
{
// do a lot more stuff but I have this for temp info
echo("hello");
// I am printing all the check box values in here I do not
// have the code for it yet but I think ik how to do it
// but since i do not have code for it now it is just empty
}
?>
hi so everytime I click on the button refresh. the isset($_POST['pass']) does not work. I have to click on it a second for it to be set which would then print the hello and table of check items part.
I want it so that if you click on it once it will print the hello. Not twice. How do i fix my code? FYI I know you could do isset($_POST['food']) for it to work. But that will break other parts of my code.
I need the button to set the isset($_POST['pass']) to be True (after one click) after I press the refresh button one time.

If exist in mysql db don't submit the form

I have a form in php where i have to check if productid and period is there in db if yes then don't submit the form.
write now I am checking
(if ($_POST[prodID'] == [productID'] && $_POST['Period']==['period'])
But how can i make select disabled or maybe the form will not get submitted if the above check is true?
You have a bracket mistake in your code, correct that
(if ($_POST[prodID'] == [productID'] && $_POST['Period']==['period'])
to
if ($_POST['prodID'] == ['productID'] && $_POST['Period']==['period'])
rest you want something like this
$submit_button = "";
if ($_POST['prodID'] == ['productID'] && $_POST['Period']==['period'])
{
$submit_button = "disabled";
}
<input type="submit" <?php echo $submit_button ?> >
this will disable your submit button and stop form from submit also , you can write a message inside your if condition so that user will know why the form is disabled

PHP - Check if page refresh or post data on the same page

Is there any way to know if the page was refreshed or data was posted data on the same page?
To be little more specific:
I have to post data on the same page.
This affects the where condition of the query.
If the page was refreshed, then the where condition must be 1.
Otherwise, where condition contains some id to get specific data from
the table.
Your best bet is to use PHP sessions, along with your submitted data in $_POST. Let's presume for this example you have the following form:
<form action="this_page.php" method="post">
<input type="text" name="important-info" />
<input type="submit" value="Submit" />
</form>
Then elsewhere in the same page is the PHP code:
<?php
// example code
session_start();
if (!isset($_SESSION['previousVisitor']) && isset($_POST['important-info'])) {
// this is a new visitor who has submitted the form
$_SESSION['previousVisitor'] = true;
// where is based on $_POST['important-info']
} else () {
// where is 1
}
// close the session after you do what you need - this stops large pages causing hang
session_destroy();
Please note that they can clear this session variable by deleting their cookies.
on the top of the page just include
if(isset($_POST['name']) && $_POST['name']!=''){
//your code goes here
}
I suggest you to check request
//Here goes the code
session_start();
$counter = 0;
$counter = (isset($_SESSION['param'])) ? $counter++ : 0;
if($counter == 0)
echo "data GET or POST";
else
echo "refreshed";
** If you want only POST param, use $_POST instead of $_REQUEST

changing value of isset

I'm wondering if it's possible to manually change the value of an isset value. That is, to do something like this:
isset($_POST['search_user']) = true;
Why I want to do this: I have two different "submit" forms on one page. When one form is submitted, I want to capture all the values of that form into SESSION variables. However, when the other form is submitted, the SESSION variables are wiped out (since the first form is not, technically, submitted anymore).
My idea was that, if the second form is submitted, then automatically set the value of the first form to true
If I understand your question correctly, if a second form is submitted, why not just destroy the current session and start new sessions using the variables posted from the new form?
http://php.net/manual/en/function.session-destroy.php
session_destroy();
...Or, you can set another session variable if the second form is submitted:
if (isset($_POST['search_user'])) {
$_SESSION['search_user'] = "true";
}
if ($_SESSION['search_user'] == "true") {
// Second form was submitted
}
You can try to define a name and a value for each submit button, so you retrieve this in the PHP file and do what you want, according you need. For instance:
HTML to the first form:
<form name="form1" action="page2.php" method="post">
<input type="submit" value="1" name="button01">
</form>
HTML to the second form:
<form name="form2" action="page2.php" method="post">
<input type="submit" value="1" name="button02">
</form>
Then you can detect the form thas was submited doing this in page2.php:
if($_POST['button01'] == "1")
{
// Do what you need based on form1 submit
}
elseif($_POST['button02'] == "1")
{
// Do what you need based on form2 submit
}
Try this and then leave some comment telling if it helps you.

checkbox status "checked" by default problem

I have a page with search form on it and table with search results below. In search form i have checkbox "Search in this category". What i'm doing to check it by default :
if(!isset($_SESSION['inthiscat'])){
$_SESSION['inthiscat'] = 'on' ;
$checked = 'checked';
}
$_GET['inthiscat'] = $_SESSION['inthiscat'];
checkbox code : INPUT type="checkbox" name="inthiscat"<?=$checked?>.
Link to next page of results index.php?inthiscat=$_GET['inthiscat'].
So the problem is when i uncheck "Search in this category" its still checked when i going to next page of results. How to fix it and what i'm doing wrong? Session startet of course.
Firstly, do you really need SESSION variables for this? If you want box to be checked when GET parameter is not specified, you do not need SESSIONs at all.
Assuming you want to preserve the behaviour in case someone removes the GET parameter:
<?php
session_start();
//......
//......
$checked='checked';
if(isset($_REQUEST['inthiscat'])) {
// Form input and url GET parameters take precedence
if($_REQUEST['inthiscat'] != 'checked') { $checked = ''; };
} else if(isset($_SESSION['inthiscat'])) {
// Next, use session variable if it exists
if($_SESSION['inthiscat'] != 'checked') { $checked = ''; };
};
$_SESSION['inthiscat']=$checked;
?>
Note:
1) Assigning values to GET array is not a good practice.
2) I assume you are using correct syntax for your FORM submit.
3) IMO, you could remove the SESSION variable as you are explicitly passing as GET parameter in the subsequent urls. Or dont use the GET parameter in urls.
Problem is: when you uncheck the checkbox and go to the next page, $_SESSION['inthiscat'] will still be unset - where did you change it?
Here is the code:
if (isset($_GET['inthiscat'])) {
$_SESSION['inthiscat'] = $_GET['inthiscat'];
}
if (!isset($_SESSION['inthiscat'])) {
$checked = 'checked';
} else {
if ($_SESSION['inthiscat'] == 'on') {
$checked = 'checked';
} else {
$cheked = '';
}
}
Assuming this HTML: <INPUT type="checkbox" name="inthiscat" checked="<?=$checked?>" value="on" />
So what it does is:
Looks for the GET data and, if there is, assigns it (can be 'on' or '') to the SESSION;
If there is no SESSION (that means, no GET as well) it's the first page of that kind the user visits, so checked;
If there is a SESSION for inthiscat, it means it's not the first page and GET data has been assigned to the SESSION. So, if it's on, it displays the mark; else, it does not.

Categories