checkbox says only "on" all the time - php

this is how I have 2 checkbox and I do not want that you should only click on them simultaneously. it must only be possible to click on one at a time!. in the present to when I clicked the news, so it must enter a number in there to write "on". I would like to later be sure it is entered into the database.
the problem is that it appears to say "on" no matter what!?
<form action="#" method="post">
<table>
<tr>
<td>Emne</td>
<td>Vigtigt: <input type="checkbox" name="vigtigt" class="new"> Nyhede: <input type="checkbox" name="nyhede" class="new"></td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="title" maxlength="50" class="new"></td>
</tr>
<tr>
<td>Tekst</td>
<td><textarea name="tekst" cols="20" rows="15" class="new"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="opret" value="Opret Blog" class="new"></td>
<td></td>
</tr>
</table>
<?php
if(isset($_POST["opret"]))
{
if($_POST["vigtigt"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
elseif ($_POST["nyhede"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
else
{
echo "Fejl!";
}
}
?>
</form>

You have to define a value attribute for it to:
<input type="checkbox" name="vigtigt" class="new" value="MyValueHere">
This way you will receive its value when its checked.
Edit:
To have only one selected at a time you will have to use type="radio" and give them the same name and different values.
<input type="radio" name="vigtigt" class="new" value="MyValueHere">
<input type="radio" name="vigtigt" class="new" value="MyOtherValueHere">

The purpose of checkboxes if to provide a group of options from which any number can be selected.
If you want a single option to be selected, use radio buttons (or a select element).
The value attribute (of the checked radio / selected option) determines the value that will be submitted. on is the default value for checkboxes which is used if you forget to specify a specific value.
<label><input type="checkbox" name="foo" value="vigtigt" class="new"> Vigtigt</label>
<label><input type="checkbox" name="foo" value="nyhede" class="new"> Nyhede</label>

Related

PHP radio button not posting correctly

I'm stuck in a bit of a pickle I can't figure out. Any help is appreciated.
I set up an HTML radio table
<div class="radio-group">
<label class="heading">Choose your game</label><br/>
<table>
<tr>
<td>
<input type="radio" name="radio" value="Radio 1"> Radio 1
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 2"> Radio 2
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 3"> Radio 3
</td>
</tr>
<tr>
<td>
<input type="radio" name="radio" value="Radio 4"> Radio 4
</td>
</tr>
</table>
</div>
</br>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset"/>
</form>
</div>
My PHP page will POST the correct selection, but loads other stuff it shouldn't. How do I differentiate between the radio selections if there are all the same name? If I name it separately, you can click on every selection.
if (isset($_POST['submit'])) {
if(isset($_POST['radio']))
{echo '<iframe etc etc></iframe>;
}
else{echo '<iframe other></iframe>;}
}
You can check what value was posted and then make your echo statement based on that. Here's an example of two radio buttons in the same group and name
<input type="radio" name="radio" value="1"> Radio 1
<input type="radio" name="radio" value="2"> Radio 2
and here is the PHP to see which one has been submitted
if(isset($_POST['radio'])) {
$value = $_POST['radio'];
if($value == "1") {
echo "SOMETHING HERE FOR VALUE 1";
} elseif($value == "2") {
echo "SOMETHING HERE FOR VALUE 2";
}
}
Notice how the value is set to something simple like an integer, it makes it easier to check it later on and then display the appropriate or desired outcome for that selected button :)

remember checkbox value if its checked array name in a form php

lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)

2 forms on same page php

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values

Multiple Values (1 or 0) for checkbox array with same name

Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked.
I can't use " input type="hidden" name="chk_name[]" value="1" " because of the array. It doubles my array.
If I try to set it after using isset it doesn't put it into my array.
I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.
So how can I put a 1 into the array for wherever it isn't checked?
Form:
<?php
require_once ("con.php");
$p=payment();
$result = mysqli_query($mysqli,$p);
while($res=mysqli_fetch_array($result))
{
?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]"
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>
php:
$paid=$_POST['paid'];
$active=$_POST['active'];
foreach($_POST as $key=>$value)
{
$ID=$ID[$key];
$paid=$paid[$key];
$active=$active[$key];
$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up);
echo "Information stored successfully";
}
?>
my function:
function updatePayment($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
:Updated code:
I can see the arrays are coming out fine for everything now. The hidden method worked.
Thanks for everyone's help!
As soon the hidden field and the checkbox has the same name it will work no matter if the name is array or not.
The simple reason for this is that when is unchecked the checkbox value is not posted.
Try this code and you will see the result
<pre>
<?php
print_r($_POST);
?>
</pre>
<form method="POST">
<input type="hidden" name="fields[1]" value="1"/>
<input type="checkbox" name="fields[1]" value="0"/>
<input type="hidden" name="fields[2]" value="0"/>
<input type="checkbox" name="fields[2]" value="1"/>
<input type="hidden" name="fields[3][something][else]" value="3"/>
<input type="checkbox" name="fields[3][something][else]" value="3"/>
<button type="submit">Submit</button>
</form>

keep radio button values and set default checked

I have a form with a radio button group. I want to set 'checked' as default the second radio button, and also keep the value after submitting form if user clicks on the first one.
Note that I'm using the <span class="custom-radiobutton"></span> for style the radiobuttons, with a lower z-index than the real <input type="radio", which has opacity:0.
This is a snippet of my code:
<div class="group-wrapper">
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
<label for="hosting-1">Sí</span>
</div>
<div class="radiobutton-wrapper boolean">
<span class="custom-radiobutton"></span>
<input type="radio" id="hosting-2" name="hosting[]" value="0" class="w100" <?php if ((!isset($_POST['hosting'])) || ((isset($_POST['hosting'])) == 0)) {echo 'checked="checked"';}; ?> />
<label for="hosting-2">No</label>
</div>
</div>
Additional info:
I'm using HTML5.
I'm validating the form with PHP (I want to keep this, even if I know maybe is better jQuery+PHP validation).
I have noticed that I need two clicks to select the first radio button. This only occurs from original state. After this, It works with one click, as expected.
I'm expending a lot of hours trying to figure out what's wrong, so any help will be very appreciated.
Cheers,
<input type="radio" id="hosting-1" name="hosting[]" class="w100" checked="checked" /> 1
try this in your code.
for example:
<tr>
<td><br><b>Reservation Handling : <span style="color:#FF0000">* </span></td>
<td><br><input type="radio" name="reservation" value="Delighted" required> Delighted<br></input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" checked="checked" value="Satisfied"> Satisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="Dissatisfied"> Dissatisfied</input></td>
</tr>
<tr>
<td> </td>
<td><input type="radio" name="reservation" value="N/A"> N/A</input></td>
</tr>
You have an error in your logic. You're using isset twice.
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> />
You're essentially saying does isset equal 1, which in your case will always be true if $_POST['hosting'] has a value.
You should do this
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if (isset($_POST['hosting']) && $_POST['hosting'] == 1) {echo 'checked="checked"';} ?> />

Categories