I want to retain the state of checkboxes after the submit button has been clicked.
What it is currently doing is that it is resetting the checkboxes after the submit button is clicked.
My code is given below:- -
<?php
-
session_start();
-
?>
-
<html>
-
<head>
-
</head>
-
<body>
-
<form name="form1" action="chk10.php" method="POST">
-
<table>
-
<?PHP
-
for($j=1;$j<=10;$j++)
-
{
-
?>
-
<tr>
-
<td>
-
<input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
-
$a=$_POST["chk"];
-
for($i=1;$i<=10;$i++)
-
{
-
$_SESSION['count'][$i]=$a[$i];
-
}
-
for($i=1;$i<=10;$i++)
-
{
-
if($_SESSION['count'][$i]==$a[$i])
-
{
-
echo 'checked="checked"' ;
-
}
-
}
-
> <?PHP echo $j; ?>
-
</td>
-
</tr>
-
<?PHP
-
}
-
?>
-
<tr>
-
<td>
-
<input type="submit" name="btn" value="save" />
-
</td>
-
</tr>
-
-
<tr>
-
<td>
-
<a href="session1.php">Show session content</a>
-
</td>
-
</tr>
-
-
-
</table>
-
</form>
-
</body>
-
</html>
4 8184 Atli 5,058
Recognized Expert Expert
Hi.
Keep in mind that a checkbox will not be sent unless it is checked.
So using the isset function is a good way to see if a box was selected.
For example: -
<?php
-
// Check if the box was sent.
-
$checked = "";
-
if(isset($_POST['box1'])) {
-
$checked = 'checked="checked"';
-
}
-
-
// Print the form
-
echo <<<END
-
<form action="?" method="post">
-
<input type="checkbox" name="box1" $checked /> Testbox<br />
-
<input type="submit" />
-
</form>
-
END;
-
?>
-
Hii,
Thanks for ur quick reply. Now my exact question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.
My question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.
The code that i have tried is below:- - code goes here...
-
<?php
-
session_start();
-
?>
-
<html>
-
<head>
-
</head>
-
<body>
-
<form name="form1" action="chk10.php" method="POST">
-
<table>
-
<?PHP
-
for($j=1;$j<=10;$j++)
-
{
-
?>
-
<tr>
-
<td>
-
<input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
-
$a=$_POST["chk"];
-
for($i=1;$i<=10;$i++)
-
{
-
$_SESSION['count'][$i]=$a[$i];
-
}
-
for($i=1;$i<=10;$i++)
-
{
-
if($_SESSION['count'][$i]==$a[$i])
-
{
-
echo 'checked="checked"' ;
-
}
-
}
-
> <?PHP echo $j; ?>
-
</td>
-
</tr>
-
<?PHP
-
}
-
?>
-
<tr>
-
<td>
-
<input type="submit" name="btn" value="save" />
-
</td>
-
</tr>
-
-
<tr>
-
<td>
-
<a href="session1.php">Show session content</a>
-
</td>
-
</tr>
-
-
-
</table>
-
</form>
-
</body>
-
</html>
-
Atli 5,058
Recognized Expert Expert
Hi.
Do not double post your questions. This only serves to cause confusion and extra work for us moderators.
I have merged your new thread into this one.
Please read the Posting Guidelines before posting.
Thank you.
MODERATOR
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jack |
last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that
builds the sql string.
In the front end asp page, the checkbox code is written as follows:
<i><input type="checkbox" name="chk_Complete" value="<%Response.Write
l_IsChecked%>"<%if cbool(l_IsChecked) then Response.Write " checked"%>>
The code to captures the checkbox value in the asp page that builds the sql
string is follows
|
by: aundro |
last post by:
Hello,
I've been looking on the web for a solution to this problem:
I create a set of checkboxes, and 2 buttons:
- one is labeled "All"
- the other is labeled "None"
Clicking "All" is supposed to check all the checkboxes, which it does
(that's not rocket science ;), but the 'onchange' event does not get
triggered.
|
by: Earl Teigrob |
last post by:
I have a page that reads values from an XML file to display to the user. The
page also has a control panel that allows administrators to update the XML
file with new values. When an administrator presses the update button in the
control panel area of the page and the values are written to the XML file, I
want the page to reload with the new values WHILE RETAINING ANY VIEW STATE
VALUES. (that are not rewriten by the new XML values)
I have...
|
by: Leo J. Hart IV |
last post by:
Hello,
I'm hoping someone can help me out.
I was wondering if the Enabled property of a CheckBox or RadioButton
server control is stored in the ViewState. If not, is there some way
to to add this property to ViewState? If needed, I can give you more
info on exactly what I'm trying to do.
Thanks,
|
by: Scott H |
last post by:
I have a page with several server controls placed on there at design
time, including a blank table control.
During runtime I add some controls to the table, including some
checkboxes, which I called ch() as checkbox. the user would then
check some of them and click a button.
the event handler for the button would save the checkbox checked state
to a database.
but as soon as the button is clicked, ch() is nothing. all the other
| |
by: clinttoris |
last post by:
Hello Experts,
I have been told to post this in the Javascript forum as I want to do
this client side just before my form gets submitted. Once the user
clicks the submit button a javascript function needs to run and
validate all the checkboxes on my form and make sure none of them are
unchecked. I suck at Javascript and my problem is 2fold. I have the
following code that constructs the checkbox
response.write "<input type=checkbox...
|
by: Jankie |
last post by:
Greetings everyone !
I seek some help to retain checkbox state on redisplay.I only have one optional checkbox to indicate whether it's the user's first visit to the site
Form1:
<INPUT type="checkbox" name="guest" value="yes">
|
by: LionsDome |
last post by:
Hello,
I have a vb.net page which a bunch of checkboxes. A user can select a
checkbox(s) and hit the submit button to store those values in a SQL
Server table. This works fine with no problem the first time the user
submits. However when user submits a second time while changing some
of the selected boxes the page only re-submits the previosly selected
checkbox values. It's like its storing it somewhere in the cache or
something. My...
|
by: ramprakashjava |
last post by:
hi,
i hav "java.lang.NullPointerException" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz..
Regards
Ramprakash
/*Adminpage.jsp*/
<%@ page language="java" import="java.sql.*"%>
<html>
<head>...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |