简体   繁体   中英

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

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!

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM