2

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!

1
  • 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. Commented Oct 28, 2015 at 14:42

2 Answers 2

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.