Checkbox array returning NULL - php

I want a batch deletion feature in the billing system I am developing, however the checkboxes, when var_dumped yields a NULL value when checked.
Here is my markup (edited):
<form method="post" name="form-countries-del">
<div id="deleteBox">
<span class="middleAlign"><b>No. of Records:</b> <span class="red"><?= $qtotal; ?></span></span>
<span class="rightAlign">
<input type="submit" value="Delete" name="submit-countries-del" class="del-submit" />
</span>
</div>
<table border="1" id="src-table">
<tr class="header">
<td><input type="checkbox" id="toggleChecks"></td>
<td class="_hide">CountryID</td>
<td>Country Code</td>
<td>Country Name</td>
<td>Telco Rate</td>
</tr>
<?php while($row = $pdo->fetch()) : ?>
<tr class="<?=($c++%2==1) ? 'even' : 'odd' ?>" title="Double click to edit">
<td><input type="checkbox" name="toDelete[]" id="toDelete[]" class="toDelete" value="<?= $row['CountryID']; ?>" /></td>
<td class="_hide"><?= $row['CountryID']; ?></td>
<td><?= highlight($_POST['search'], $row['CountryCode']); ?></td>
<td><?= highlight($_POST['search'], $row['CountryName']); ?></td>
<td><?= $row['Trate']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</form>
The generated form (Google Chrome) is like this:
It just yields NULL unfortunately. I wonder what I've been missing. I've been scanning a whole lot of similar articles regarding my problem but none has worked thus far.
I hope you folks can help me!
Cheers!

Until you check the checkbox you will get the value as NULL, Once you checked the checkbox and try with post method you will retrieve the value.

You have bad html.
Move the <form method="post" name="form-countries-del"> to before <div id="deleteBox">

Your form close(</form>) not defined in properly.
Your html code will generate like this, <span> <form> ......(without ending form) </span>
The Html parse the If any unclosed tags presents it will automatically added closing tag inbetween.
like this,
<span >
<form>
<!-- Submit button -->
</form> <!-- added automatically -->
</span>
<!-- Check box elements goes here -->

Some MVC frameworks mess around with the request variables such as POST. It's possible this is the case with you. As your submit button is posting ok, it could be just a problem with POSTing arrays. Try this to get around that problem.
<input type="checkbox" name="toDelete-<?= $row['CountryID']; ?>" id="toDelete[]" class="toDelete" value="<?= $row['CountryID']; ?>" /></td>
<td class="_hide"><?= $row['CountryID']; ?>
You can access the values like so:
$toDelete = array();
foreach($_POST as $p){
$ex = explode('-',$p);
if($ex[0]=='toDelete'){
$toDelete[] = $ex[1];
}
}

Related

Delete multiple rows using checkbox in Codeigniter

I have a table, which is fetching data from database correctly, Now I want to add a checkbox in front of each row in the table and submit button above the table. When I check on some checkboxes, I want to delete those rows, I was trying this code to get the ids of the selected rows, but I get nothing when I press the submit, Can you please help me out?
My View file:
<?= form_open('user/set_archive'); ?>
<input type="submit" value="Submit">
<table>
<thead>
<td>Action</td>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</thead>
<tbody>
<?php foreach ($emails as $email) : ?>
<tr class="read">
<td>
<input type="checkbox" name='archiveval[]' id="ch<?= $email['id']; ?>" value="<?= $email['id']; ?>" >
<label for="ch<?= $email['id']; ?>"></label>
</td>
<td>....</td>
<td>....</td>
<td>....</td>
</tr>
<?php endforeach; ?>
<?= form_close(); ?>
</tbody>
</table>
My Controller user/set_archive
public function set_archive()
{
$data1 = $this->input->post('archiveval');
echo "<pre>";
print_r($data1);
}
I believe your assignment to id is incorrect. You can get ids of the rows selected like this
<input type="checkbox" name='archiveval[]' id="archiveval[]" value="<?= $email['id']; ?>" >
Give it a shot

Automatically click the button for you if there is a date

I have a file called file.php which has:
$row2= pg_fetch_array($result2);
$website = 'http://validate/?id='.$newDateString;
<tbody>
<tr>
<td><?php echo'<a href="'.$website.'">'. $row['name']; ?></td>
The code sends to a file named jasmine.php the url with the date since jasmine contains a calendar. It sends it when you click on the date which contains a link to jasmine.
Jasmine.php:
<div id="content">
<h2>Hello, Please let me know which date you are looking for </h2>
<div><label style="font-size: 14pt; font-family: Verdana;">Posting Date: </label></div><br />
<input type="text" id="date" value= "<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's work!" class="btn" />
<input type="button" id="clear" value="Wanna go Again?" class="btn" />
</div>
<div id="contentarea2" style="display: none;">
<h2>Results:</h2>
<div class="view_alerts">
<table>
<tr>
<td>
Code
</td>
<td>
# Posted
</td>
</tr>
<tbody id="view_alerts_tbody">
</tbody>
</table>
</div>
</div>
In Jasmine, I get the date already written for me from file.php, all I need to do is click the button so it could show me a table, is there a way, that it clicks the button on its own if theres a date written, so when I go on that link, it already puts the date and takes me to the table in jasmine.php
Taking the code from the answer above the if statement here should evaluate the input fields value (id is '#date'). An empty value will default to false hence the ! before the selector.
jQuery(document).ready(function()
{
if(!$('#date').val()) {
jQuery("#Your-Button-ID").click();
}
});
You can use below JQuery code for your purpose. When page load is completed (i.e. Page is ready)
jQuery(document).ready(function()
{
jQuery("#Your-Button-ID").click();
});

Create multiple forms in a html table

I've got a MySQL database and a table called courses. I would like to show the records of this table within an html table, but I would also like to edit them with a submit button at the end.
I know this has been asked before, but I'm still struggling with how I should properly proceed.
I currently have:
<tbody>
<?php
foreach ($db->getResultSet() as $row) {
extract($row);
?>
<tr>
<form id="myTable" name="editrecord" action="course.php" method="post">
<input type="hidden" name="code" value="<?php echo $code; ?>" /><!--PrimaryKey-->
<td><?php echo $code; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
<td><input type="text" name="desc" value="<?php echo $desc; ?>" /></td>
<td><input type="submit" name="Edit" value="Edit!" /></td>
</form>
</tr>
<?php
}
?>
</tbody>
Now this seems to work fine. However I'm aware that <form> should not be inside <tr> or anywhere else other that <td>
I'm aware I could use various divs and set display properties to table layout etc. however I'd like to avoid this as I am using pre-made javascript that can sort and filter data which requires me to use <table>
I also tried a for loop outside the table to create 1 form per record and use the primary key as the form's id. Then proceed with echo "<td><input form='$code' .... But this doesn't work in Explorer
What is the correct way to implement something like this?

How to retrieve and display data using a PHP function within an HTML form

I have been working on this for probably 20 hours of research and trial/error with no solution in sight I figured I'd try here and see if someone can finally point me in the right direction and give me some solid advice.
Here's the setup. I have a page (customer_search.php) that has an HTML form, I want the user to be able to search the DB by $last_name and the results be displayed on a table on the same page.
First: IS THIS POSSIBLE? I have read so much over the past few days, I doubt myself but then I think it can be done without using Java and using purely PHP/HTML.
I also use MVC model.
THIS IS THE MAIN PAGE (customer_search.php)
<?php include '../view/header.php';
?>
<div id="main">
<h1>Customer Search</h1>
<div id="content">
<form action="" method="post" id="aligned"
<label>&nbsp Last Name</label>
<input type="text" name="last_name"/>
<br />
<label> <label>
<input type="submit" value="Search" />
</div>
<div id="content">
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<tr>
<td><?php echo $_POST['firstName'] .' '. $_POST['last_name']; ?></td>
<td><?php echo $_POST['email']; ?></td>
<td><?php echo $_POST['city']; ?></td>
<td><form action="customer_display.php" method="post">
<input type="hidden" name="action" value="get_customer" />
<input type="hidden" name="customer_id"
value="<?php echo $_POST['customer_ID']; ?>" />
<input type="submit" value="Select" />
</form>
</td>
</tr>
</table>
<br />
</div>
</div>
<?php include '../view/footer.php'; ?>
THIS IS customer_db.php in the MODEL folder which contains a function that I'd like to use, get_customers_by_last_name($last_name)
<?php
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
ORDER BY lastName';
$customers = $db->query($query);
return $customers;
}
function get_customers_by_last_name($last_name) {
global $db;
$query = "SELECT * FROM customers
WHERE lastName = '$last_name'
ORDER BY lastName";
$customers = $db->query($query);
return $customers;
}
I apologize for the OVERLOAD of code, but this is my first post on here and I've tried everything I can think of. I just want to search the DB by last name on the html form and then have the results displayed on the table (on the same page) and I don't care if the page has to refresh. I know PHP is server-side and not client so it needs to refresh. I just can't seem to get the results to actually display in the table, if I could just get the results there, the next step is to SELECT the customer which passes it to the next page customer_display.php but I'll cross that bridge when there. Thanks for your help, and I really am begging for some help/lessons on this. I'm desperate!!!! :(
Hi you did your homework and are correct that the page HAS to be refreshed.
Unless you plan to use an Ajax call with javascrcipt (jQuery for exemple)
To do what you ask, you have to put the PHP at the beginning.
You put it in an If() statement that will only be valid if the form has been posted.
Here is what I would do with your code:
<?php
if(isset($_POST['last_name'])){
include_once 'customer_db.php';
$customers = get_customers_by_last_name($_POST['last_name']);
}
include '../view/header.php';
?>
<div id="main">
<h1>Customer Search</h1>
<div id="Search">
<form action="" method="post" id="aligned"
<label>&nbsp Last Name</label>
<input type="text" name="last_name"/>
<br />
<label> <label>
<input type="submit" value="Search" />
</form>
</div>
<?php if(isset($customers)){ ?>
<div id="Results">
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<tr>
<?php while ($a_customer = mysql_fetch_assoc($customer)) { ?>
<td><?php echo $a_customer['firstName'] .' '. $a_customer['last_name']; ?></td>
<td><?php echo $a_customer['email']; ?></td>
<td><?php echo $a_customer['city']; ?></td>
<td><form action="customer_display.php" method="post">
<input type="hidden" name="action" value="get_customer" />
<input type="hidden" name="customer_id"
value="<?php $a_customer['customer_ID']; ?>" />
<input type="submit" value="Select" />
<?php } ?>
</td>
</tr>
</table>
<br />
</div>
<?php }?>
</div>
<?php include '../view/footer.php'; ?>
Some crucial advices:
Use Mysqli (or PDO) library instead of Msql for php
you should not trust user input data on your search form you can use the janitor.class to sanitize the post.
When you have more time check these links concerning PHP security : Evil User, PHP security , PHP Secure coding
Oh! and don't give two elements in the same page the same id Value the purpose of the id is to give each element a unique identifier.
I have had success doing this same thing in the past using JQuery and jTable.
This uses AJAX calls which allows you to call PHP scripts without reloading the page.
You can find out everything you need to know here: http://www.jtable.org/
Use AJAX to make a call to a PHP object that instantiates your model and returns your results in a JSON string. Upon receiving the results, use a JavaScript loop to iterate through the JSON and output your table.
I recommend jQuery, which has its own .ajax() function, and it's easy to manipulate the DOM with your result set. Go read the jQuery docs.
This is what AJAX is for... asynchronous communication with the server (i.e. not on a page load). Your best bet would probably be to use JQuery or another JS framework, which will make the AJAX calls a snap: $.ajax();:
Something like:
$.ajax({
type: "POST",
url: '/directory/to/php_script_that_outputs_the_table_html.php',
data: { name: "Smith"},
success: function(data) {
$('.class_of_div_to_recieve_code').html(data);
}
});

Having Trouble Calling WordPress Plugin Form Value

My Plugin form code:
function pp_settings_page() {
$pp_options = get_option('pp_options');
?>
<div class="wrap">
<h2>Post Products Settings</h2>
<form action="options.php" method="post">
<?php settings_fields('pp_options_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Currency sign: </th>
<td><input type="text" name="pp_options[currency_sign]" value="<?php echo $pp_options['currency_sign']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save changes" />
</p>
</form>
</div>
<?php
}
I have tried to call it within the template files using:
<?php $pp_options=get_option('pp_options');?>
and
<?php get_option('pp_options');?>
What am I missing?
I don't see any code to handle the form submit, more specifically, code which extracts the post variable and saves it using update_option
Your action would need to be changed to the settings page URL so when it gets posted it runs the next bit of code, which you would put inside your pp_settings_page function.
if(isset($_POST['field_name'])) {
$field_value = $_POST['field_name'];
update_option('option_name', $field_value);
}

Categories