i'm trying to do a poll but i designed it without any radio box in it. So i decided to make the selection being highlighted with a different background color, all is done with jquery.
I set the display of the radio box to none so that it wouldn't show, gave each a unique ID. Here's the script.
<form action="v_poll.php" method="post">
<ul class="voting">
<li class="voting votetext"><input type="radio" name="voting" value="a1" style="display:none;" id="a1"><a onClick="vote('a1')"Answer 1</a></li>
<li class="voting votetext"><input type="radio" name="voting" value="a2" style="display:none;" id="a2"><a onClick="vote('a2')">Answer 2</a></li>
<li class="voting votetext"><input type="radio" name="voting" value="a3" style="display:none;" id="a3"><a onClick="vote('a3')">Answer 3</a></li>
<input type="hidden" value="1" name="id" />
<input type="submit" value="submit">
</ul>
</form>
<script type="text/javascript">
function vote(TheValue) {
GetElementById(TheValue).checked=true;
}
</script>
But when i checked the value of the radio box with $_POST['voting'], it is blank. Not the value assigned to the radio box. Anything i'm doing wrong?
Please help. Thanks.
You can do this without javascript as well, using a <label> with for="id" instead of <a>, like this:
<li class="voting votetext">
<input type="radio" name="voting" value="a1" style="display:none;" id="a1">
<label for="a1">Answer 1</label>
</li>
This is nothing new/HTML5 or anything, any browser IE6+, and probably older will support this.
GetElementById(TheValue).checked=true;
should instead be:
document.getElementById(TheValue).checked=true;
Related
I have many radio buttons with different name attribute and I am facing one problem. Every time I click on each radio button, I can select all. I am using the radio buttons for redirecting to other pages.
I don't want to have multiple selection just only one. I am using twitter bootstrap tabs for showing multiple content on one page just by switching the tabs if that has something to do with the prob. Can someone help me out?
php
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['cleat']) && !empty($_POST['cleat'])) {
header("location: sporting_goods");
exit();
}
if (isset($_POST['cell']) && !empty($_POST['cell'])) {
header("location: cellphones");
exit();
}
}
?>
css
.radto{vertical-align:top;}
.sell_ali{display:inline-block;}
html
<form action="" method="post">
<div class="tabbable tabs-left" style="margin-bottom: 18px;">
<ul class="nav nav-tabs">
<li class="active">Electronics</li>
<li>Sporting Goods</li>
</ul>
<div class="tab-content" style="padding-bottom: 9px; border-bottom: 1px solid #ddd;">
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="camera"/> Cameras</li>
<li><input type="radio" class="radto" name="cell"/> Cell Phones</li>
<li><input type="radio" class="radto" name="cell_cover"/> Cell Phone Covers</li>
</div>
</div>
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="ball"> Balls</li>
<li><input type="radio" class="radto" name="cleat"> Cleats</li>
<li><input type="radio" class="radto" name="jersey"> Jersey</li>
</div>
</div>
</div>
<input type="submit" value="next" name="submit">
</form>
Give then the same name. That is how you create a radio button group.
Use the value for the piece of data that the user is picking from that group.
If you want to allow just one choice you should give them same name buddy. then set the value you want to send to server for each one.
Here is the snippet of html code.....
1)After checking each check box the value's is posting to the database.
2)But the problem is when i checked other, i need to take the value of the text box and it is posting the value of the checkbox instead of the textbox value
but i don't know where i did mistake...
<form action="purchase.php" name="form1" id="form1" method="POST">
<ul class="n_ul"> <span>*</span>
What is your Primary goal?
<br>
<br>
<li>
<input name="goal" id="goal" value="Add a popular customer service to attract/retain more customers"
type="checkbox">
</li> <span>*</span>
Popular customer Services
<br>
<br>
<li>
<input name="goal" id="goal" value="Add a turnkey revenue sources for my location(s)"
type="checkbox">
</li> <span>*</span>
trunkey revenue source
<br>
<br>
<li>
<input name="goal" id="goal" type="checkbox" value="other">
</li> <span>*</span>
Other (Please specify below)
<br>
<br>
<input name="other" id="goal" type="text" class="new">
</ul>
<input type="submit" name=submit value="submit">
</form>
Any suggestions are acceptable....
Try something like
$_POST['goal'] = ($_POST['goal']=='other') ? $_POST['other'] : $_POST['goal'];
This will overwrite the value of goal with the value of other only when the 'other' radio is ticked
Also id attributes of html elements should be unique on the page
EDIT
Your question is a little vague. It seems like you may want the form to submit when a checkbox button is clicked.
If this is the case the other field will be unlikely to be filled as the form will often be submitted before the user gets to populate it
Try adding a button or input to submit the form
Like this
<button type="submit">Submit</button>
You can check the value of your checkbox, and if it equals to other, you can grab your value from textbox. Do it following way:
if (isset($_POST['goal']) && $_POST['goal'] == 'other')
{
// do something with $_POST['other']
}
Try this
<form action="" name="form1" id="form1" method="POST">
<ul class="n_ul"> <span>*</span>
What is your Primary goal?
<br>
<br>
<li>
<input name="goal[]" id="goal" value="Add a popular customer service to attract/retain more customers"
type="checkbox">
</li> <span>*</span>
Popular customer Services
<br>
<br>
<li>
<input name="goal[]" id="goal" value="Add a turnkey revenue sources for my location(s)"
type="checkbox">
</li> <span>*</span>
trunkey revenue source
<br>
<br>
<li>
<input name="goal[]" id="goal" type="checkbox" value="other">
</li> <span>*</span>
Other (Please specify below)
<br>
<br>
<input name="other" id="goal" type="text" class="new">
</ul>
<input type="submit" name=submit value="submit">
</form>
<?php
$other="";
$goal=$_REQUEST['goal'];
if(in_array("other", $goal)){
$other=$_REQUEST['other'];
}
echo $other;
?>
If I had some coding as follow.
<form method="POST" action="localhost/carts/delete">
Item 1
Item 2
Item 3
</form>
And I like to post to a form with some hidden value to indicate which Item is clicked, how to do that???
Thanks.
Edited Text.
Thanks for so many useful suggestions.
I actually use Laravel 4 rather than PHP, my Laravel code produced this HTML
<form ...>
<ul>
<li>
<h1>Key: 1 </h1>
<input name="cart_id" type="hidden" value="1">Delete
</li>
<li>
<h1>Key: 2 </h1>
<input name="cart_id" type="hidden" value="2">Delete
</li>
<li>
<h1>Key: 6 </h1>
<input name="cart_id" type="hidden" value="6">Delete
</li>
<li>
<h1>Key: 7 </h1>
<input name="cart_id" type="hidden" value="7">Delete
</li>
</ul>
</form>
So, when I clicked on any items, I got 7 which is the last value of cart_id, how to place at the right place, in JavaScript???
Create a hidden element inside the form:
<form method="POST" action="localhost/carts/delete">
<input type="hidden" name="myValue" value="" id="myValue"/>
Item 1
Item 2
Item 3
</form>
Then on click over any link (a) change its value and submit the form.
$('a').click(function(e){
//preventing the default link redirection
e.preventDefault();
$('#myValue').val($(this).data('value'));
$(this).closest('form').submit();
});
Use a hidden input field like:
<input type="hidden" value="hidden value" name="id"/>
This box is not visible in your page.
Use input fields instead.
<form method="POST" action="localhost/carts/delete">
<input type="submit" value="Item 1" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
</form>
Then in PHP you can retrieve the clicked value like this:
$_POST["whichitem"]
If you are worried about the styling, simply add it in your css:
input[type="submit"]{
//style
}
I have a database with pictures that i can delete from or use them in different parts of the the website by choosing one of the radio buttons in the popup.Problem is everytime I click I click on a picture the name of the first picture in the database comes up not the one from the picture i clicked . What's wrong? I used PHP 5.3 and HeidiSQL
<?php
// ...
$result=mysql_query("SELECT * FROM imagini WHERE menu_id=6");
while($data=mysql_fetch_row($result)){ ?>
<div class="tag">
<div id='container_poze_originale'>
Alege alt rol
<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
close
<form action="popup.php" method="post" >
<input type="text" name="alt-rol" value="<?php echo $data[1];?>" /> //always returns the name of the first picture in the database not the one i select
<input type="submit" value="Adauga imagine" class="buton_imagine" />
<div class="radio">
<input type="radio" name="tip_imagine" value="0"/><label for="tip_imagine" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1"/><label for="tip_imagine" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2"/><label for="tip_imagine" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3"/><label for="tip_imagine" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4"/><label for="tip_imagine" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5"/><label for="tip_imagine" class="radio2">Background</label>
</div>
</form>
</div>
<div class="imagine_originala">
<img src= "../upload/original/<?php echo $data[1];?>" /></a>
</div>
<div class="Btag" style="display:none;">
<div id="buton_slide4" >
Sterge</td>
</div>
</div>
</div>
</div>
<?php
}
?>
I think I understand your question. You are clicking on
Alege alt rol
and you are surprised that the same <div> always appears?
Well, it is simply because an id should be unique. And if you cycle through your images you create a new <div> each time with the same id: my_popup. When clicking on the link (no matter which one), JavaScript will then just take the first <div> it finds with the id my_popup. So you should just assign an unique id to each div so your JavaScript knows which one to open. You could do this by appending your image id to the div id (assuming you have a unique image id in $data[1]).
So change it to (shortened):
<? while($data=mysql_fetch_row($result)) { ?>
<div class="tag">
<div id="container_poze_originale">
Alege alt rol
<div id="my_popup<?=$data[1]?>" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
...
</div>
</div>
</div>
<? } ?>
Oh and it might be good to put all those huge inline CSS attributes into an external stylesheet, so it will be easier to maintain/change your webpage later on. And it makes things a bit more readable... :-)
I think your form is ok when you click on the radio but not when you click on the label.
The for attribute of the label need an ID as reference like this :
<input type="radio" name="tip_imagine" value="0" id="tip0" /><label for="tip0" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1" id="tip1"/><label for="tip1" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2" id="tip2"/><label for="tip2" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3" id="tip3"/><label for="tip3" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4" id="tip4"/><label for="tip4" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5" id="tip5"/><label for="tip5" class="radio2">Background</label>
With your actual code, the FOR is always the same so you get always the first.
I want to check all the check boxes which are in div tag
My code is as below.
<div id="cBoxes">
<li><input class="radio" name="list" id="list" type="checkbox"
value="<?php echo $resultPages['id'];?>"></li>
</div>
I have put this whole code in for loop with php tag.
Is there any way to accomplish this task.
Set the checked attribute of checkbox. w3.org has complete documentation.
See the checked attribute:
<div id="cBoxes">
<ul>
<li><input class="radio" name="list" id="list" type="checkbox"
value="<?php echo $resultPages['id'];?>" checked="checked"/></li>
</ul>
</div>