Is it possible to submit a checkbox form without submit button in php?

10,147

Solution 1

Add javascript function submit() in onchange

<form method="POST" action="?">
<p><input type="checkbox" name="dori" value="dori" onchange="this.form.submit()"><em> 0 - 5000</em></p>
<p><input type="checkbox" name="bora" value="bora" onchange="this.form.submit()"><em> 5000 - 1000</em></p>       
</form>

Solution 2

I implemented the submission of multiple checkbox value by using php as follows. It works perfectly for me and hope that it will work for everyone .

<?php
session_start();


$samsung=NULL;
if(!empty($_POST['samsung']))
$samsung = $_POST['samsung'];
$_SESSION["samsung"] = $samsung;
$samsung1 = $_SESSION["samsung"];
echo $samsung1."<br />";

$apple=NULL;
if(!empty($_POST['apple']))
$apple = $_POST['apple'];
$_SESSION["apple"] = $apple;
$apple1 = $_SESSION["apple"];
echo $apple1."<br />";

$low=NULL;
if(!empty($_POST['low']))
$low = $_POST['low'];
$_SESSION["low"] = $low;
$low1 = $_SESSION["low"];
echo $low1."<br />";

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Checkbox</title>
</head>
<body>
<form  action="checkbox.php" method="post">
Samsung<input type="checkbox" name="samsung"  value="samsung" onchange="this.form.submit()" <?php if(!empty($_SESSION["samsung"])){echo "checked";} ?>>
Apple<input type="checkbox" name="apple"  value="apple" onchange="this.form.submit()" <?php if(!empty($_SESSION["apple"])){echo "checked";} ?>>
5000-1000<input type="checkbox" name="low"  value="low" onchange="this.form.submit()" <?php if(!empty($_SESSION["low"])){echo "checked";} ?>>
</form>

</body>
</html>

Here I use session variable so that once I checked Samsung it will persist even if I checked apple . Hope this example will help you.

Share:
10,147
Doss Smart
Author by

Doss Smart

Updated on June 05, 2022

Comments

  • Doss Smart
    Doss Smart almost 2 years

    I am a php newbie and i am trying to create a price range system for my ecommerce website project.

    so I did a checkbox form:

    <form method="POST" action="?">
    <p> <input type="checkbox" name="dori" value="dori"><em> 0 - 5000</em></p>
    <p><input type="checkbox" name="bora" value="bora"><em> 5000 - 1000</em></p>
     <input type="submit">         
     </form>
    

    For that no problem, i can handle it...But what i want to achieve is if the user ticked the box, i can get the value of the box ticked without a submit button clicked.

    How can i achieve that?

    This is the form i want to achieve

    <form method="POST" action="?">
    <p> <input type="checkbox" name="dori" value="dori"><em> 0 - 5000</em></p>
    <p><input type="checkbox" name="bora" value="bora"><em> 5000 - 1000</em></p>       
     </form>
    

    Hope i explain clearly...Thanks in advance for the help!

    • Marc B
      Marc B over 8 years
      to submit a form, you either have to hit enter, or click a submit button. so what you want will require javascript to trap the onclick.
  • Aaron Gong
    Aaron Gong over 8 years
    any change in the state will trigger a submit, your can also replace the javascript with alert('...'); or console.log('...'); to see something firing off when state is change.