Making value checkbox appear - php

I have a problem with my code. I use CodeIgniter.
I made a checkbox form with a code like this :
foreach ($get_student->result_array() as $row) {
$select_student[$row['student_number']] = $row['student_name'];
echo form_checkbox($select_student,$student_number,FALSE);
}
then the result just like this, only the checkbox appears, the student's name or student number does not appear:

As per the document,
Find the CodeIgnitor form_checkbox documentation
the array should have an element with key='value'.
Hence this line of code
$select_student[$row['student_number']] = $row['student_name'];
should be changed to
$select_student['value'] = $row['student_number'];

Related

Hide html value inside input field

I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.
Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.
Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';

Getting dynamic dropdown value to a Session in PHP

a PHP newbie here and need to clarify a thing.
I have populated a dropdown box with data from a SQL database. The code looks like like below.
echo '<select id="dropMe" name="dropMe" style="width:150px; font-family:Georgia;">';
echo '<option value=""></option>';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
What I want to know it is it possible to assign a value selected by a user to a SESSION hence this is a dynamic dropdown? (saveroute is my submit button)
if (isset($_POST['saveroute']))
{
$Q = $_POST['dropMe'];
$_SESSION['menuRoute'] = $Q;
echo ($_SESSION['menuRoute']);
}
I code something like this, but I get an undefined error with 'dropMe'. I'm not quiet familier with this type of error and can some one throw some suggestions or point out any errors in the method.
Thanks for looking.
Try printing out the whole POST array with
print_r
Like this
print_r($_POST);
It looks like you are checking if "saveroute" is set and not "dropMe", looks like it doesn't exist.
http://php.net/manual/en/function.print-r.php
I would recommend you to inspect the $_POST array to be sure that you are receiving the value you are expecting from the html form.
var_dump($_POST);
or
print_r($_POST);
If that is correct then be sure that you have started the session before accesing it with:
session_start();

Single dynamic values submitted through one form

I am using Jquery UI Selectable. The user has the option to dynamically add new list items to the original setup.
I would like to include a 'clear' button that will give the user the ability to clear each individual item they created by clicking on an X input submit (img).
HTML (php)
if ($this->session->userdata('inactivefilter') == true) {
$inactivefilter = $this->session->userdata('inactivefilter');
$i=0;
foreach ($inactivefilter as $filter)
{
$filterdash = implode('-', explode(' ', $filter));
echo "<li class='ui-state-default' id='$filterdash'>$filter</li>";
echo "<div id=clear>
<form method='POST' action='".base_url()."main/clear_filter'>
<input type='image' name='filtervalue' value='$i' src='".base_url()."img/board/icons/clear.png'></input>
</form>
</div>";
$i++;
}
}
This is where the list is created. specifically the clear_filter action form.
Clear_filter currently 'attempts' to grab the value of '$i' but I don't know how to pass that correctly.
here is the controller:
public function clear_filter()
{
$i = $_POST['filtervalue'];
$this->thread_model->clear_filter($i);
}
I'll omit the clear_filter model due to its irrelevance to the problem.
Essentially, I just need $i to be picked up based on whatever value is clicked on in the dynamic form on the actual page.
Any help would be appreciated!
Well, it seems like I just had things a bit backwards.
The code was more or less correct.
For Codeigniter, you catch the passed input value=$i by using the name ="filtervalue"
Change the controller code to :
$i = $this->input->post('filtervalue');
and $i is set to whatever value was clicked on.

how to keep value in a listbox in PHP?

I got a listbox in my php page that generate from another listbox, the code is as follows
<?php $myArray1 = $_POST['countryRF'];?>
<select name='countryRF[]' id='countryRF' size='10' multiple style='width:180px' class='selfont'>
<?php
foreach($myArray1 as $value){ // Loop through each element
print "<option value=\"".$value."\">".$value."</option>";
}
?>
</option></select>
While refreshing the form listbox get empty, how can I keep added values even after form reload ?
Is it possible to keep the mysql result table stable even after form reloading by session ? If so please give me a help ?
i see you get the 'countryRF' from post.
you can store it in a cookie/session, of store it on the server.
use
if (isset($_POST['countryRF'])) {
$_COOKIE['countryRF'] = $_POST['countryRF'];
}
$myArray = $_COOKIE['countryRF'];
same goes with session
save them into Sessions

PHP + MsSQL = Checkbox problems. Need HELP!

I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!

Categories