Getting radio button value from view to controller in codeigniter - php

I have reviewed and tried the answers already given on this forum but nothing worked. So now I am sharing my issue here. I want selected radio button value from view to controller to do further action on the basis of selected radio button. But its not working, receiving blank result. My view and controller code is below
<form>
<div class="inline">
<input class="magic-radio" type="radio" name="conn_ty" value="12m" id="12m" checked />
<label for="12m">
12 month terms
</label>
</div>
<div class="inline">
<input class="magic-radio" type="radio" name="conn_ty" value="24m" id="24m"/>
<label for="24m">
24 month terms
</label>
</div>
<div class="inline hidden">
<input class="magic-radio" type="radio" name="conn_ty" value="36m" id="36m"/>
<label for="36m">
36 month terms
</label>
</div>
</form>
Contoller:
$connection_term = $this->input->post('conn_ty');
echo $connection_term;

from your view:
Use echo form_open($url); instead of form tag and create a submit button.
On your controller
$connection_term = $this->input->post('conn_ty');
echo $connection_term;

put this on your view
echo form_open('on controller which function u want to get value');
and you have to declare
$RADIO_VALUE = array('name' => 'radio button name','id' => 'radio button id');
don't forget
echo form_close();

Related

Radio button only returns "on" when submitted

I am very new to PHP and I am trying to display the value of a selected radio button in a string. But all I get returned is "on" instead of the value associated with the selection. Any help would be great!
<form action="Handle_form_PHP.php" method="post">
<div class= "row">
<div class ="col-25">
<label for="student">Student</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value ="student">
</div>
</div>
<div class= "row">
<div class ="col-25">
<label for="student">Employee</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value = "employee">
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
This is the PHP I am using to print:
// Create a shorthand for the form data:
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$address = $_REQUEST['address'];
$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$student = $_POST['student'];
// Print the submitted information:
echo "<p>Thank you, <b> $student </b>, <b>$fname $lname</b>, for the filling out our survey:<br>
<p>We will Send you your report at: <i>$address</i>, <i>$state1</i><i>$state2</i>.</p>\n";
If your $_POST['student'] superglobal variable was holding a value of 'on', then the issue has nothing to do with method case-sensitivity.
The default value for a radio button is 'on' unless you provide a value attribute inside the radio button tag.
Let me prove it to you. Go to [cringe] https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox then paste the following snippet in the left-side box, then Run, then Submit.
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
<input type="radio" name="student" checked> Student<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is submitting with a method of get (notice the lowercase get is inconsequential. post will behave the same way). See that without a value in the student radio input, the submitted value is on.
If you write value="Red42" inside the radio input, you will no longer receive on; the submitted value is Red42.
use POST not post when saying method:
<form action="Handle_form_PHP.php" method="POST">
not
<form action="Handle_form_PHP.php" method="post">
For the record, the book was incorrect and had "post"

Dropdown value is not selected on submitting form in php

The scenario is - I have a form with multiple input fields and text areas and whenever I post this form all these values will get posted. However, I also have a dropdown that I create from my database. This dropdown value shows up properly when I get the values. I have a second submit button, which should take this selected value and post to other page.
I have tried the $_POST='name of select option' but it did not help.
I have a onlick for directing the form to other page for updating the db.
I'm fairly new to php, so it could be the use of _POST that could be incorrect.
<form name="f1" class="formoid-solid-blue" method="GET">
<div class="title">
<h2></h2>
<h2>Tracking & Receiving</h2>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="small" type="text" name="store" placeholder="Store #"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="medium" type="text" name="userid" placeholder="UserId"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="large" type="text" name="order" placeholder="Order Number"/>
<span class="icon-place"></span>
</div>
</div>
<div class="submit">
<input type="submit" name="Send" value="Send"/>
</div>
<div class="element-separator">
<hr>
<h3 class="section-break-title">Tracking Numbers</h3>
</div>
<div class="element-multiple">
<label class="title"></label>
<div class="item-cont">
<div class="large">
<select data-no-selected="Nothing selected" name="updTR" multiple="multiple">
<option name="op" value="<?php require 'connection.php'; ?>"> //getting value form db
<?php
echo $trackID; //DB value
$trackID = $_GET['updTR']; //getting the variable from the form
?></option>
</select>
<span class="icon-place"></span>
</div>
</div>
</div>
<div class="submit">
<input type="submit" onclick="f1.action='UpdateTR.php'; return true;" name="UpdateTR" value`enter code here`="Submit"/>
</div>
</form>
Well, looking at the form, you said you are using POST, and tested with other POST related method, but your form is using GET, as seen on your code above.
Since you are new to PHP, as an example, if you are using post, and a variable is waiting on the other page to collect this information from your form then you do it like this:
This is the form field example:
<input type="text" name="XYZ">
Then on the page that will collect this info, it would be
$someVariable = $_POST[ 'XYZ' ];
now, if you want to use GET, then its the same thing but you use this
$someVariable = $_GET[ 'XYZ' ];
hope this clears the confusion.
-- EDIT 2 --
Ok after reading your comment, and since i haven't seen how you are iterating through your DB for the options that go in that "option" list/menu, I'm going to say you cant put "connection" as the value on this part:
<option name="op" value="<?php require 'connection.php'; ?>">
because assuming that "connection.php" is connecting to the DB, then that wont help you, that goes elsewhere. Instead, once you've made that connection(elsewhere, preferably above it in the header somewhere), you then have to have code that loops through the DB, and "WHILE" its looping, spit out results into that options field. Let me give you an example with PSEUDO code.
If a normal select/option code looks like this
<select>
<option> option 1 </option>
<option> option 2 </option>
</select>
etc, then you need php that loops through your DB results, and puts the results into the "options" in that select,
here is some pseudo code:
<select>
<?php
while($row = your fetch code here){
echo "<option>your line items here</option>";
}
?>
</select>
OR.....
if "$row" has a specific value you want to use from the database that you visually want to add in that list, then you could do similar to above but with something like:
<select>
<?php
while($row = your fetch code here){
echo "<option>your line items here "' . $row['some value from DB here'] . '"</option>";
}
?>
</select>
etc.
Essentially, you want your "while loop" to iterate through your database and WHILE its doing it, to input its data into your options html.
ALSO, if you wanted a particular value from your DB, put into a GET variable, to use for the processing page (as i mentioned all the way above), then again, similarly, you can do something like this:
<?php
while($row = your fetch code here){
echo "<a href='linkHere.php?yourVariableToTrackWithGetHere='".$row['yourDBresutlToTrack']."'> Your text here </a>"; }
?>
This way, when you click the link, the database value gets added to that variable that you can call later in the processing page.
Kinda long winded but, this should set you 100% straight.
Hope this helps.

Using $_GET to recieve the labels off of radio buttons

I want to print the label off of my radio button to a seperate page, using something similar to this using php
<div class="radio"><?php echo "-" . (!empty($_GET['radio']) ? $_GET['radio'] :'');?>
That will be on the other page, but this is the code with the radio buttons on the first page.
<div class = "radio" required="required">
<h3 style = "margin-top:-20px;">Meaningless text.</h3>
<label for="x"><input type="radio" name="x" id = "x" /> <span>ex.1</span></label><br>
<label for="y"><input type="radio" name="x" id = "x" /> <span>ex.2</span></label><br>
<label for="z"><input type="radio" name="x" id = "x" /> <span>ex.3</span></label>
</div>
I have this div in a form, and the form redirects to the page with the $_GET when the submit button is clicked. I want the ex.1, 2 or 3 to be printed when the option for it has been clicked. I have tried, and i am stumped. Help please!
Why not set the value attribute to what you're after...
<label for="x">
<input type="radio" name="x" id="x" value="ex.1">
<span>ex.1</span>
</label>
<label for="y">
<input type="radio" name="x" id="y" value="ex.2">
<span>ex.2</span>
</label>
<label for="z">
<input type="radio" name="x" id="z" value="ex.3">
<span>ex.3</span>
</label>
I also fixed your duplicate id attributes. I assumed from the <label> from attribute, they were meant to be x, y and z.
The value of $_GET['x'] (assuming <form method="GET">) will be the value of the checked radio button.
<div class="radio">-<?= !empty($_GET['x']) ? $_GET['x'] : '' ?></div>
Modify your HTML as
<input type="radio" name="x" value="ex.1" id="1" />
This way, your $_GET array will have the value ex.1 at index x. You can print it using
<?php if(isset($_GET['x'])) echo $_GET['x']; ?>
Another thing I would like to point out is that you really shouldn't be using same values for id. It is supposed to be unique per page, and other elements like CSS and JS depends on you giving unique values for id.
Good Luck!

How to add extra filters to a searchresults page in codeigniter?

In my Codeigniter website I've made a searchform. When I search the page is redirected to a 'searchresults' page.
I always get a lot results and I want to minimize that. I would like to add searchfilters to the 'searchresults' page, so I can search on specific categories/keywords.
I've been searching for this the whole day.
What I've tried is adding a dropdown and a input field to the searchresults page. The dropdown is populated with the categories found with the normal searchform. This works, but I can't filter and limit the searchresutls when I submit the 'second searchform'.
My form on the 'searchresults' page looks like this:
<script type="text/javascript">
$(document).ready(function(){
$(".zoekfilters").hide();
$(".filters").show();
$('.filters').click(function(){
$(".zoekfilters").slideToggle();
});
});
</script>
<div class="zoekfilters">
<br/>
<form action="home/get_filtered_vac" method="post">
<p class="field">
<label class="field">Categorie:</label>
<select name="catselect" id="selection" value="Categorie">
<option selected disabled value="">Selecteer een categorie</option>
<?php foreach($all as $dat ){ ?>
<option value="<?php echo $dat['categorie']; ?>"><?php echo $dat['categorie']; ?></option>
<?php } ?>
</select>
</p>
<br/>
<p class="field">
<label class="field">Tags:</label>
<input name="tag" type="search" placeholder="Tags">
</p>
<br/>
<p class="field">
<label class="field">Aantal uren:</label>
<?php foreach($all as $dat){ ?>
<?php echo $dat['uren']; nbs(2); ?>
<input type="checkbox" value="<?php echo $dat['uren']; ?>">
<?php } ?>
</p>
<br/>
<p class="field">
<input type="submit" value="Filter resultaten">
</p>
</form>
You can try it yourself, so the idea is clear:
Go to: http://kees.een-site-bouwen.nl/vacatures
leave the big searchbox empty and add '9101' to the small searchbox, Now wait a second an then submit the form. You will be redirected to http://kees.een-site-bouwen.nl/vacatureresults
Click on 'Zoekfilters' to open the second searchfrom on the 'searchresults' page.
If people need my model functions I will post them.

How to store and recall the last radio button clicked in a session variable

I'm trying to figure out the best way to display text based on which radio button a user has clicked. I am having trouble using sessions to do this. When I use the following code, only the information related to the radio button that was first clicked is displayed (e.g., if someone clicks on radion button "A," then changes his mind and clicks on radio button "B," the session seems to think "A" is still clicked.
Any suggestions?
Here's the HTML:
<div class="radio">
<label for="choice1">Choice 1</label>
<input class="selection" id="choice1" type="radio" name="selection" value="choice1"/>
<div class="radio">
<label for="choice2">Choice 2</label>
<input class="selection" id="choice2" type="radio" name="selection" value="choice2" />
</div>
Here's the PHP code:
if (!isset($_SESSION)) {
session_start();
$_SESSION['formStarted'] = true;
//...
if(($_SESSION['selection']) == 'choice1'){echo 'Text to be included only if choice 1 was selected.';}
$_SESSION['selection'] = isset($_POST[selection]) ? $_POST[selection] : 'choice1';
OR $_GET or $_REQUEST

Categories