If exist in mysql db don't submit the form - php

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

Related

Checked box should stay checked 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.

Insertion of data with two buttons

Actually my problem is,I have fours forms in two pages. I have two buttons name save and continue. Here if I click on save button data submitted in one table and show list of details ,when I click on another button called continue it will go to another page .
Here is the following code -
if (isset($_POST["submit_x"]) || !empty($_POST["submit_y"])) {
//here submit_x is input name of save button and submit_y is input name of continue button
/* runs some code of insert query */
if($submit_y=="continue"){
header("Location: example.php");
}else{
header("Location: example.php?action=list");
}
}
Can any one help me please.
Thanks in advance
Create two submit inputs with the same name and different values:
<input type="submit" name="action" value="Continue" />
<input type="submit" name="action" value="Save" />
the form will send the value of the clicked button:
<?php
if (isset($_POST['action'])) {
if ($_POST['action'] == 'Continue') {
...
}
}
<?php
if (isset($_POST['action']) && !empty($_POST['action'])) {
if ($_POST['action'] == 'Continue') {
//write code for continue part
}
if ($_POST['action'] == 'Save'){
//write code for Save part
}
}
?>
Here you have to make clear that when you will submit the form you will insert the form step wise step or all at a time......if you will go all at a time then you have store your values in session or else if you want to go through step wise step then in second form you have to update the second step fields with the first step id.
if (isset($_POST["submit_x"]) || !empty($_POST["submit_y"])) {
$_SESSION['name']=$_POST['name'];
$_SESSION['address']=$_POST['address']
}
like this you need to store in session and take it to next step

Simple PHP form submitting - what's wrong with this conditional if and logical &&?

The problem:
if i submit the html form and a textbox is left blank
then i don't want it to proceed to the echo segment,
but the problem is it does proceed.
<?php
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
{
?>
<form name="form2" method="post" action="<?php echo($_SERVER["PHP_SELF"]);?>">
<div>
Write a planet name: <input name="planet" type="text"><br>
Its moon: <input name="moon" type="text">
</div>
<div>
<input type="submit" name="submit" value="submit">
</div>
</form>
<?php
}else{
echo("Planet: ".$_POST['planet']. "<br>");
echo("Moon: ". $_POST['moon'] . "<br>");
echo "Successful.";
}
?>
As you know isset() determines if a variable is set and not null but doesn't check if it's empty.
While logic seems my if statement, I modified it from:
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
To:
if(!isset($_POST['submit']) && ($_POST['planet']=='') && ($_POST['moon']==''))
if(!isset($_POST['submit']))
if(!isset($_POST['planet']) && !isset($_POST['moon']))
if(empty($_POST['moon']) && empty($_POST['planet']))
and none of them worked.
So am I doing something wrong with my if statement? how can I not let it proceed to the Else segment while a textbox is empty? without more if and no nested statements please.
When you submit a form, the submit button will be set, so isset($_POST['submit']) will be true, therefore !isset($_POST['submit']) will be false.
When doing an if statement with the && comparison, all conditions must be true in order to execute that block of code, otherwise it goes to the else statement.
What you need to do is actually have 2 comparison checks. Once to see if the form was never submitted and one to see if it was, and the text boxes are empty:
<?php
// Check if form was submitted
if(!isset($_POST['submit'])
{
// Display the form
}
else
{
// Form was submitted, check if values are empty
if(trim($_POST['planet'])=="" || trim($_POST['moon'])=="")
{
// One or more value is empty, do something
}
else
{
// Process form
}
}
?>
I realize you are trying to avoid nesting, but in order for the logic to flow smoothly, and the code to remain readable, this is a necessary evil.
Change
if(!isset($_POST['submit']) && empty($_POST['moon']) && empty($_POST['planet']))
To
if(!isset($_POST['submit']) || (empty($_POST['moon']) || empty($_POST['planet'])))
Then if you submit with either textbox being empty, it will redisplay the form. If both textboxes have been filled in, you will see the else part.
Your problem is that if(!isset($_POST['submit'])) is always set when the form is submitted - so that is true. You also might want to change the && to ||. By using || you say OR, so say if anyone is empty, then do this, else do that.

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.

PHP function problem

I have a php function script that is supposed to uncheck a checkbox or checkboxes if a user unchecks it when the preview button is clicked but I can only get the last
checkbox that was unchecked to stay unchecked but not the other checkeboxes how can I fix this so that all the checkboxes that where unchecked stay unchecked?
Here is part of my PHP function that is giving me the problem.
if(isset($_POST['preview'])){
foreach($query_cat_id as $qci) {
if(!in_array($qci, $cat_id)){
$unchecked = $purifier->purify(strip_tags($qci));
}
}
}
for ($x = 0; $x < count($query_cat_id); $x++){
if(($query_cat_id[$x] == $cat['id']) && ($cat['id'] != $delete_id) && ($cat['id'] != $unchecked)){
echo 'checked="checked"';
}
}
Why not just just check if the variable is set, if the checkbox is checked when the form is submitted, it will be available in $_POST['checkboxName'], otherwise, isset($_POST['checkboxName']) will return false.
Basic script to test it
<?php
if (isset($_POST['heh']))
echo $_POST['heh'];
else
echo "Not checked";
?>
<form action='yourPage.php' method='post'>
<input type='checkbox' name='heh' />
<input type='submit' />
</form>
View it in action
http://robertsquared.com/so.php
i solve this problem on client side
i have a input of type hidden with the same name as the checkbox with the value of 0 and the checkbox right after the hidden field with the value of 1
if the checkbox is not checked i get the value of 0 from the hidden field and if someone checks i got the 1 from the checkbox.
so i only need to check if $value==1 then checked=checked

Categories