Pass or Post the input value without using form - php

I'm working right now on how I can pass the value of <input id="sub" name="sub"> that has a value of first 3 letters of dropdown option text. I need to pass or post the value of <input id="sub" name="sub"> to process my query below.
When I choose for example the ITEquipment in dropdown, the <input name="sub"> get the first 3 letters of dropdown option text. So now it has the value of ITE, that ITE what I need in my query. In query for example, $sub=$POST["sub"] is equal to ITE so I used LIKE function, LIKE '$sub-__' . Hope you understand what I'm trying to do.
How I can do that without using submit form?
Here's my example process of what I'm working http://jsfiddle.net/xqGLS/1/
Help please?
<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
$resultcode = $mysqli->query("SELECT category, id, maincode FROM category GROUP BY id ORDER BY maincode");
$code = '';
while($row = $resultcode->fetch_assoc())
{
$code .= '<option value = "'.$row['maincode'].'">'.$row['category'].'</option>';
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<br/>
Category
<select name="maincode" style="text-transform:uppercase;" onchange = "GetChangedValue(this);">
<option value=""></option>
<?php echo $code; ?>
</select>
</br>
Sub Code
<input type="" name="sub1code" id="sub1code" value="" readonly style="width:45px;text-transform:uppercase;">
<script>
$('[name="maincode"]').change(function() {
$('[name="sub1code"]').val($(this).val());
var input = $('[name="sub"],[name="sub1"]'),
input1 = $('[name="sub"]'),
input2 = $('[name="sub1"]'),
input3 = $('[name="equal"]');
input.change(function () {
input3.val(input1.val() + input2.val());
});
});
</script>
<script>
function GetChangedValue(e) {
var value = e.options[e.selectedIndex].text;
var elem = document.getElementById("sub"); elem.value = value.substring(0,3);
}
</script>
<input name="sub" id="sub" value="" style="width:35px;text-transform:uppercase;" readonly>
--My Query--
<input id="sub1" name="sub1" style="width:35px;text-transform:uppercase;" value='
<?php
$result2 = $mysqli->query("SELECT * FROM code
WHERE sub1code LIKE 'SUP-___' ORDER BY sub1code");
while($row = $result2->fetch_assoc())
{
$value = $row['sub1code'];
}
$first = substr($value, 0, 4);
echo $first;
$last = substr($value, -3);
$i="0";
while($i<=$last)
{
$i++;
}
$value2=strlen($i);
echo $first;
if($value2==1)
{
echo "00".$i;
}
elseif($value2==2)
{
echo "0".$i;
}
else
{
echo $i;
}
?>'>
<input id="equal" name="equal" value="" style="width:60px;text-transform:uppercase;" type="hidden">
<input type="submit" name="">
</form>

HEAVILY EDITED
$(document).ready(function(){
$("#dd").change(function(){
var v1 = $(this).val().substring(0,3);
$.ajax({
type : 'get',
url : 'call.php',
data : {varReq:v1}
}).done(function(returnResult){
alert(returnResult);
});
});
});
call.php would contain your php/mysql script. To get the value passed through there should be via $_GET or $_POST. But you have to include the proper type in your jquery's ajax too.
So in my example, What I have on my php, just a dummy one.
<?php
if(isset($_GET['varReq']))
{
if($_GET['varReq'] == 'ABC') echo "AAAA";
else echo "BBBB";
}
?>
It pass this html element...
<h1>Example</h1><p>BBBB</p>
Then pop up message (alert) will be called showing the return value.

Related

Check whether input values are equal

I want to use this code for a barcode scanner as follows:
The scanned barcode is entered in the insert_code input and then I want to display "code is ok", when the value in search_code = insert_code.
My code, after validation, clears the input search_code and it is annoying to have to reintroduce the same code in search_code every time again.
What can I do to keep the value in search_code after each validation?
<form action="" method="post">
Cod I:<input type="text" name="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" value=""/><br/><br/>
<input type="submit" name="button" value="validation" />
</form>
<?php
$search_code = $_POST ["search_code"];
$insert_code = $_POST ["insert_code"];
if ($search_code == $insert_code){
echo "code is ok";
} else {
echo "code is not ok";
}
?>
If you want to keep search_code input filled with the last submitted value, just echo the value of this post into the input if it was set:
<form action="" method="post">
Cod I:<input type="text" name="search_code" value="<?php echo isset($_POST['search_code'])?$_POST['search_code']:'' ?>"/><br/><br/>
Cod II:<input type="" name="insert_code" value=""/><br/><br/>
<input type="submit" name="button" value="validation" />
</form>
Also, to avoid warnings about undefined index, add this condition to your PHP code (check if those posts are set):
<?php
if(isset($_POST ["search_code"]) && isset($_POST ["insert_code"])){
$search_code = $_POST ["search_code"];
$insert_code = $_POST ["insert_code"];
if ($search_code == $insert_code){
echo "code is ok";
}else {
echo "code is not ok";
}
}
?>
You can do this without PHP, which will give a better user-experience. Here is a live example, just run to see it work:
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
result.textContent = search_code.value == insert_code.value
? 'code is ok'
: 'code is not ok';
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
insert_code.oninput = function () {
result.textContent = ''; // clear result;
};
<form action="" method="post">
Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
<input type="submit" id="button" name="button" value="validation" />
</form>
<div id="result"></div>
The test is done in Javascript, which responds to the button click and cancels the form's submission to the server (return false).
As a bonus the "ok/not ok" message is cleared as soon as you type a new value in the second input box.
How to use this code
Here is how the code should look in your document:
<body>
<form action="" method="post">
Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
<input type="submit" id="button" name="button" value="validation" />
</form>
<div id="result"></div>
<script>
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
result.textContent = search_code.value == insert_code.value
? 'code is ok'
: 'code is not ok';
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
insert_code.oninput = function () {
result.textContent = ''; // clear result;
};
</script>
</body>
Note that the content part has some differences to yours: every input has an id attribute, and there is an extra div.

Show input tags using JavaScript in the <div> tag

I have a question regarding javascript and cascading dropdown.
So on the main page I have 3 dropdowns and I to show the result from the dropdowns. each of the dropdown has their own processing page and then we got all the values from the dropdowns, the program will show 3 input tags with javascript for the value processing.
The problem is I cant make the javascript to work when the div is displayed.
Should I put the script in the main page or inside the page that has content to be displayed in the main page's div section?
As for right now I am putting the script on the content page
so on the content page,
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var
total = parseInt($('#quantityRequired').text()),
inputs = $('input[type="number"]');
inputs
.attr('max', total)
.change(function() {
//Make sure that current value is in range
if($(this).val() > parseInt($(this).attr('max'))) {
$(this).val($(this).attr('max'));
} else if ($(this).val() < parseInt($(this).attr('min'))) {
$(this).val($(this).attr('min'));
}
//Get currently available total
var current = available();
//Now update max on each input
$('input').each(function(indx) {
$(this).attr('max', parseInt($(this).val()) + total - current);
});
});
function available() {
var sum = 0;
inputs.each(function() {
sum += parseInt($(this).val());
});
return sum;
}
</script>
</head>
<body>
<?php
$projectName = strval($_GET['project']);
$thicknessValue = intval($_GET['thicknessValue']);
$baseplateValue = strval($_GET['baseplateValue']);
$query = "SELECT QTY_REQUIRED, QTY_CUT FROM COMPONENT
WHERE THICKNESS = :thicknessVal
AND PROJECT_NAME = :projectName
AND BASE_PLATE = :baseplateVal
AND REQUEST_STATUS = 'OPEN'";
$result = oci_parse($conn, $query);
oci_bind_by_name($result, ":projectName", $projectName);
oci_bind_by_name($result, ":thicknessVal", $thicknessValue);
oci_bind_by_name($result, ":baseplateVal", $baseplateValue);
oci_execute($result);
?>
<?php
while ($row = oci_fetch_array($result, OCI_BOTH)){
$qtyAvailable = $row['QTY_REQUIRED'] - $row['QTY_CUT'];
echo '<span id="quantityRequired">'.$qtyAvailable.'</span>';
echo '<input id="cncQty" name="cncQty" type="number" min="0" value="0" placeholder="CNC" required>';
echo '<input id="scatorQty" name="scatorQty" type="number" min="0" value="0" placeholder="SCATOR" required>';
echo '<input id="manualQty" name="manualQty" type="number" min="0" value="0" placeholder="MANUAL" required>';
echo '<br/>';
}
?>
</body>
</html>
the script is already working in jsfiddle to distribute the value into 3 input tags.
I am able to show the input tags but the java script just doesn't work.
please help me...
You need to print the PHP code in the tag of the HTML page:
</head>
<body>
<?php
while ($row = oci_fetch_array($result, OCI_BOTH)) {
$qtyAvailable = $row['QTY_REQUIRED'] - $row['QTY_CUT'];
echo '<span id="quantityRequired">'.$qtyAvailable.'</span>';
echo '<input class="form-control" id="cncQty" name="cncQty" type="number" min="0" value="0" placeholder="CNC">';
echo '<input class="form-control" id="scatorQty" name="scatorQty" type="number" min="0" value="0" placeholder="SCATOR">';
echo '<input class="form-control" id="manualQty" name="manualQty" type="number" min="0" value="0" placeholder="MANUAL">';
echo '<br/>';
}
?>
</body>
</html>

Post form values where Field Names are dynamically created from a loop?

I have a page that is created dynamically from the information that a user has submitted on a previous page. For example on page 1, the user inputs some department names. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. I need to know how to post these items once the submit button in each section is clicked. I have tried several different ways and nothing is working. I want it so that ONLY the items with the same $a value as the submit button's $a get posted.
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a;?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
<?php
echo "</div>";
}
?>
*EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. *
**EDIT 2 :
I figured it out using #dirt 's jquery suggestion.
HTML:
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a ?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>
<?php
echo "</div></div>";
} ?>
jQuery:
<script>
$(document).ready(function() {
$('#<?php echo $a ?>').submit(function() {
.post("include/action.php")
});
});
</script>
PHP:
if(isset($_POST['areaSub'])) {
$areaval = intval($_POST['areaSub']);
$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]);
echo $areaval;
echo $area_name;
echo $bdep;
}
EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button.
Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. Multiple buttons with the same name can have different values and the values don't have to be the labels.
Example:
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
# _POST:
if (isset($_POST['submit']) && $_POST['submit'] == '1') {
echo 'Department 1';
}
if (isset($_POST['submit']) && $_POST['submit'] == '2') {
echo 'Department 2';
}
?>
You could also use jQuery to get all elements contained in a certain Div ID. Something like (this is rough idea):
<div id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>
jQuery:
$('#<?php echo $a; ?>').submit(function() {
do_something_with_form_$a
});
I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? And you only want the input fields for that specific area submitted?
Section off forms with the tag:
<form> </form>
So, you'll have multiple areas:
while(...) {
<form method="..." name="form $a">
<input name="... $a">
<input name="... $a">
...
<submit>
</form>
}
(Obviously this is pseudocode, adjust accordingly)

save dynamically generated textbox & dropdown list data for one testcode into database

I have create one form which contains Test code,Test name,Test parameter are Text box & Instrument code is drop down list & also contain add another button on click of add another button text box for new Test parameter & drop down list for new Instrument code will be generated i want to save data of that newly generated Test parameter text box data & newly generated Instrument code drop down list data for single Test code..
I have create but it can not save dynamically generated new multiple text box data & generated Instrument code drop down list data please guide me.
Here is my PHP code:
<?php
global $Hostname;
global $Username;
global $Password;
global $Database_name;
function getConnection()
{
$Hostname = "localhost";
$Username ="root";
$Password ="";
$Database_name="labdata";
$oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name);
return($oMysqli);
}
include_once "dTestCreation.php";
if(isset($_POST['submit']))
{
$Testcode = $_POST['testcode'];
$TestName = $_POST['testname'];
$Testparameter = $_POST['testparameters'];
$Instrumentcode = $_POST['instrument_code'];
$InsertQuery = "INSERT INTO demo_test(testcode,testname) VALUES('$Testcode','$TestName')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery);
$InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$Testparameter','$Instrumentcode')";
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery1);
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row() //create function create_row
{
var newtr=document.createElement("tr"); //variable for tr
var newtd=document.createElement("td"); //variable for td
var newtd1=document.createElement("td"); //variable f
var output="<input type=\"text\" name=\"testparameters[]\">";
/*var output1="<select name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
var output1="<select id=\"instrument_code\"><?php include_once "dTestCreation.php"; //include file dTestCreation.php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}//display values of instrument into list.
?></select>";
newtd.innerHTML=output; //display first td
newtd1.innerHTML=output1; //display second td
newtr.appendChild(newtd); //increment no of rows.
newtr.appendChild(newtd1);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for='Testcode'>Testcode</label>
<input type="text" name="testcode"></input>
<label for='TestName'>TestName</label>
<input type="text" name="testname"></input><br></br>
<label for='Testparameter'>Testparameter</label>
<input type="text" name="testparameters"></input>
<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
<?php
$tResult = getIname(); for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremenet code hereted value display in listbox.
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}`enter code here`
?>
</select>
<table>
<tbody id="table1body">
<tr>
<!--<td><input type="textfield" name="testparameters"></td>-->
<td> <input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> <!--call function create_row() for onclik add new row-->
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>
1) You missed name attribute into your automatically generated dropdown list so without name attribute you will not get data into php script.
2) Your newly generated text box name = testparameters[] which array so you can't get value just using $_POST['testparameters']. TO get value, you have to loop through array.
foreach($_POST['testparameters'] as $value) {
// here , you get dynamically added texbox values.
}
Again try loop also but still value cant be stored into cant not count drop down list value &
& testparameters textbox save Array keyword
what i Try Here is My code:
<?php
include_once "dTestCreation.php";
if(isset($_POST['submit'])) //when clikc on submit button
{
$Testcode = $_POST['testcode'];
$TestName = $_POST['testname'];
$Testparameter = $_POST['testparameters'];
$Instrumentcode = $_POST['instrument_code'];
$InsertQuery = "INSERT INTO demo_test(testcode,testname) VALUES('$Testcode','$TestName')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery);
//print_r($InsertQuery);exit();
$length = count($_POST['testparameters']);
for($i=0;$i<$length;$i++)
{
//echo $_POST['testparameters'][$i];
$a = $_POST['testparameters'][$i];
$na = array('testparameters' => $a['testparameters']);
foreach($na as $k => $v)
{
$na[$k] = mysql_real_escape_string($v);
}
$TestParameters = $na['testparameters'];
//$TestParameters = $_POST['testparameters'][$i];
$InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$Testparameter','$Instrumentcode')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery1);
}
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row() //create function create_row
{
var newtr=document.createElement("tr"); //variable for tr
var newtd=document.createElement("td"); //variable for td
var newtd1=document.createElement("td"); //variable f
var output="<input type=\"text\" name=\"testparameters[]\">";
/*var output1="<select name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
var output1="<select id=\"instrument_code\" name=\"instrument_code\"><?php include_once "dTestCreation.php"; //include file dTestCreation.php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}//display values of instrument into list.
?></select>";
newtd.innerHTML=output; //display first td
newtd1.innerHTML=output1; //display second td
newtr.appendChild(newtd); //increment no of rows.
newtr.appendChild(newtd1);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for='Testcode'>Testcode</label>
<input type="text" name="testcode"></input>
<label for='TestName'>TestName</label>
<input type="text" name="testname"></input><br></br>
<label for='Testparameter'>Testparameter</label>
<input type="text" name="testparameters"></input>
<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
<?php
$tResult = getIname(); //call function getIname() included in file dTestCreation.php
for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremented value display in listbox.
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>"; //Instrument values display in listbox
}
?>
</select>
<table>
<tbody id="table1body">
<tr>
<!--<td><input type="textfield" name="testparameters"></td>-->
<td> <input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> <!--call function create_row() for onclik add new row-->
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>

Make hidden input value depend on radio button selection using PHP/JavaScript

I'm trying to figure out how to make the logic in the following form work. Basically if either of the first two radio buttons is checked, make the hidden input named categories have a value of vegetables. Else, make the hidden input named categories have a value of fruits.
I'm not sure if this should be done with PHP or JavaScript, but if it is done with PHP I think the form would have to be submitted to itself to be pre-processed and then the collected, pre-processed information would be sent to external_form_processor.php. If this is how you do it, what would be the PHP code that I need to use to make it work?
<?php
if($_POST["food"]=="carrots" || $_POST["food"]=="peas") {
$category = "vegetables";
} else {
$category = "fruits";
}
?>
<form name="food_form" method="post" action="external_form_processor.php" >
<fieldset>
<input type="radio" id="carrots" name="food" value="carrots" />
<input type="radio" id="peas" name="food" value="peas" />
<input type="radio" id="orange" name="food" value="orange" />
<input type="radio" id="apple" name="food" value="apple" />
<input type="radio" id="cherry" name="food" value="cherry" />
</fieldset>
<input type="hidden" name="categories" value="<?php $category ?>" />
</form>
If using jQuery would be easier, how could I call the variable as the value of the hidden input if I use the following in the head of the page?
$(function(){
$('input[name=food]').click(function(){
var selected_id = $('input[name=food]:checked').attr('id');
if (selected_id == 'carrots' || selected_id == 'peas') {
var category = "vegetables";
} else {
var category = "fruits";
}
});
});
Any help with this would be greatly appreciated. Thanks!
I think jQuery would work perfect for you, you just need to pass the category value to the input field:
$(function(){
$('input[name=food]').click(function(){
var selected_id = $('input[name=food]:checked').attr('id');
if (selected_id == 'carrots' || selected_id == 'peas') {
var category = "vegetables";
} else {
var category = "fruits";
}
$('input[name=categories]').val(category);
});
});
I would set the category in PHP when the form is submitted.
//validate inputs... always
$food = "";
if(isset($_GET['food'])){
$food = preg_replace("/[^a-zA-Z]+/", "", $_GET['food']);
}
$category = ($food=="peas"||$food=="carrots")?"vegetables":"fruits";

Categories