I am in a silly position where I can't figure out to get the values of the checked checkboxes.
<form id="civilForm" method="POST" action="form.php" enctype="multipart/form-data">
<p>
<label>
<input id="12D" name="programsRequested[]" type="checkbox" />
<span>12D</span>
</label>
</p>
<p>
<label>
<input id="xp" name="programsRequested[]" type="checkbox" />
<span>XPStorm</span>
</label>
</p>
<p>
<label>
<input id="autoTurn" name="programsRequested[]" type="checkbox" />
<span>AutoTurn</span>
</label>
</p>
<p>
<label>
<input id="hecras" name="programsRequested[]" type="checkbox" />
<span>HEC RAS</span>
</label>
</p>
Then I am using a php loop as there are a bunch more checkboxes coming.
It spins through fine, but only give me a list that says: on, on, on which correctly tells me how many I checked, however does not give me the value of the checked box.
$selectedPrograms = 'None';
if(isset($_POST['programsRequested']) && is_array($_POST['programsRequested']) && count($_POST['programsRequested']) > 0){
$selectedPrograms = implode(', ', $_POST['programsRequested']);
}
Is there something obvious I missing on how to get the values here?
add every input element value
<input id="12D" name="programsRequested[]" type="checkbox" value="1" />
form not closed .
submit button also missing .
<input type="submit" name="submit" >
Related
I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..
I'm using a html form to edit sql db, thus I would want to have the form display the value currently in the database. I'm Using a while loop to display all the sql rows. I simple radio button allowing user to chose between 'Listings' (shows as '0' in sql) or 'Recent Transactions" (shown as '1' in sql).
The radio buttons are not pre-filling the value from sqlas checked or not
<form name="edit listing" action="edit_list.php" method="post" id="edit_form" >
<ul>
<?php while ($data=mysqli_fetch_assoc($result)):
$transaction = $data['transaction'];
$chkvalue='checked="checked"'; ?>
<li>
<fieldset>
<legend>Designation <h6>Required</h6></legend>
<input name="transaction" type="radio" tabindex="11" value="0" <?php if ($transaction == '0') echo $chkvalue; ?> />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" <?php if ($transaction == '1') echo $chkvalue; ?> />
<label for="recent_transactions">Recent Transactions</label>
</fieldset>
<input type="submit" formaction="edit_list.php" value="Submit Changes" />
</form>
</li>
<?php endwhile;mysqli_close($con);?>
</ul>
The short php insert is working, a little. My source code is looking something like this:
<input name="transaction" type="radio" tabindex="11" value="0" />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" checked="checked" />
<label for="recent_transactions">Recent Transactions</label>
But the radio button is not pre-filling.
I've been poking at this all day, any suggestion here where problem could be would be extremely helpful
You only have to write checked not check="checked"!
So change it to this:
$chkvalue='checked'; ?>
instead of this:
$chkvalue='checked="checked"'; ?>
Okay so I have a script which is basically like an order form. However my only input options are radio buttons. When I run my script, I noticed that it turned back errors. Instead I would like it to display and empty field. The page posts back to itself and it updates a box I left blank on the bottom before submission. After submission details are filled in from a multitude. I also used a two dimensional array if that makes any difference. So my problems are with empty variables in the forms. I was not able to fix this and I think it might be the way I'm using it. I get an undefined index error for the variables that receive the data. In this case $settype and $differentype come out as errors since they are defined to be whatever was in the textbox. I thought that by using empty() I could eliminate that issue. I did not approach it correctly of course. Can anyone give me some guidance here? Thank you.
<form name="form1" method="POST" action="order.php">
<input type="radio" name="set" value="1" /> 1
<input type="radio" name="set" value="2" /> 2
<input type="radio" name="set" value="3" /> 3
<input type="radio" name="different" value="a" /> a
<input type="radio" name="different" value="b" />
<input type="radio" name="different" value="c" />
<input type="submit" name="submit1" value="login" />
</form>
<?php
if(isset($_POST['submit'])) {
$settype = $_POST['set'];
$differentype = $_POST['different'];
if ($settype == '1' && $differenttype =='a'){
$order = $field[0][1];
}
if (empty($settype) && empty($differenttype)){
$order = "";
}
}
?>
<table>
<tr>
<td>
<?php print($order); ?>
</td>
</tr>
</table>
Where is your input submit ? like this <input type="submit" name="submit" value="submit" />
EDIT 1: You should have a default radio button selected value like
<input type="radio" name="set" value="1" checked="checked"/> 1
<input type="radio" name="different" value="a" checked="checked" /> a
I have this page (code below) which contains a series of 5 checkboxes, all representing different values, and i need to use them to query a database.
<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
<p>Select The <b>Section</b> You Wish To Search In Below... </p>
<p>
<label for="section"></label>
<label for="section2"></label>
<label>
<input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
Functional</label>
<label>
<input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
Technical</label>
<label>
<input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
Commercial</label>
<label>
<input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
Implementation</label>
<label>
<input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
Innovation</label>
<br />
</p>
<p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
<p>
<label for="kword"></label>
<input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
<br />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
</p>
</form>
as you can see each has its own value which is the term used to query the database. the php query code on the results page is as follows
<?php
$kword = $_POST["kword"];
$section = $_POST["SectionSelect"];
function boldText($text, $kword) {
return str_ireplace($kword, "<strong>$kword</strong>", $text);
}
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and section_name like '%$section%'
and keywords like '%$kword%';")
or die(mysql_error());
?>
ultimately what i want this to do is to query the database with the query to potentially have where clauses for each of the checkbox values? for example i want to select x where y like 'Technical' AND y like 'Functional' and so on...
Any help would be great
thanks
Set up your checkbox names as array-keys to use them as an array with values of all selected checkboxes:
<input type="checkbox" name="SectionSelect[0]" value="Functional" />
<input type="checkbox" name="SectionSelect[1]" value="Technical" />
echo $_POST['SectionSelect'][0]; // should print "Functional"
echo $_POST['SectionSelect'][1]; // should print "Technical"
For some reason my javascript won't execute when my submit button is pressed. It's supposed to leave an error message beside two textfields for a user's name and email address if they are empty, but it's not and I can't figure out why. Normally, when the two boxes are filled, the submit would go to a php page which sends an email. Any suggestions or help with fixing my javascript problem will be greatly appreciated.
This is my javascript:
//my javascript function
<script type='text/javascript'>
function validate_form()
{
$('span.error_message').html('');
var success = true;
$("#validate_form input").each(function()
{
if($(this).val()=="")
{
$(this).next().html("* You must complete this field"); // the error message
success = false;
}
});
return success;
}
</script>
and my form in html:
<form action=".....whatever....." method="POST" id="validate_form" onsubmit="return validate_form();">
<ol>
<li>
<span id="question">___ is your name? My name is Marie.</span>
<input type="text" name="q1" id="q1" />
</li>
<li>
<span id="question">___ are you from? I'm from Paris, France.</span>
<input type="text" name="q2" id="q2" />
</li>
<li>
<span id="question">Dave: ___ you like football</span>
<p>
<input type="radio" name="q6" value="1" id="q6_1" />
<label for="q6_1">Are</label>
<input type="radio" name="q6" value="2" id="q6_2" />
<label for="q6_2">Do</label>
<input type="radio" name="q6" value="3" id="q6_3" />
<label for="q6_3">Does</label>
<input type="radio" name="q6" value="4" id="q6_4" />
<label for="q6_4">Is</label>
</p>
</li>
<div id="username">
<p>
Before we begin, please enter your name and email:
<br />
<label for="user_name">Name: </label><input type="text" name="user_name" id="user_name" />
<span class="error_message" style="color:#FF0000"></span>
<br />
<label for="user_email">Email: </label><input type="text" name="user_email" id="user_email" />
<span class="error_message" style="color:#FF0000"></span>
</p>
</div>
<p style="width: 242px; margin: auto;">
<input type="submit" name="submit" value="Submit your answers" id="but" />
</p>
</form>
Edit: Fixed the problem with the javascript being called, but now the submit button isn't working even with the two textfields being filled.
$("#validate_form input") - I don't see validate_form anywhere; you need to add it or just use: $("input")
Firebug for firefox would help you debug these types of jscript issues. You can also use the Error Console in Firefox if you dont want to install firebug.