I have web page in PHP which displays all records in a table. I want to add check boxes against all rows and user can check a check box to select a row and then submit the page. When the page is submitted I want to enumerate all check boxes and check whether they are checked or not, How can I do this?
You'll create your checkboxes like this:
<input name="rows[]" value="uniqueIdForThisRow" type="checkbox" />
<input name="rows[]" value="anotherId" type="checkbox" />
Then you can loop through them like this:
<?php
// $_POST['rows'] contains the values of all checked checkboxes, like:
// array('uniqueIdForThisRow', 'anotherId', ...)
foreach ($_POST['rows'] as $row) {
if ($row == 'uniqueIdForThisRow') {
// do something
}
}
?>
PHP docs on dealing with forms, see especially Example #3.
Creating the form
You can generate the HTML as follows:
<form [action, method etc]>
<table>
<?php
foreach($dataSet as $dataRow) :
?>
<tr>
<td>
<input type="checkbox" name="dataRow[]" value="<?=$dataRow['id']?>"/>
</td>
[Additional details about datarow here]
<tr>
<?php
endforeach;
?>
</table>
</form>
AFTER POST
look into $_POST['dataRow'] : this will be an array with values the IDS of your $dataRow, so using array_values on $_POST['dataRow'] will give you all the ids of the selected rows:
<?php
$checkedRows = array_values($_POST['dataRow']);
foreach($checkedRows as $row) {
// Do whatever you want to do with the selected row
}
You don’t have to check all checkboxes if they have been checked. Because only successful controls are send to the server. And a checkbox is only successful when it’s checked:
Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.
So you just have to look what checkboxes you get in the request at all. And if you want to use <select multiple>, take a look at How do I get all the results from a select multiple HTML tag? in the PHP FAQ.
if i were you... i wouldn't fight with altering html table structure.
you can handle that with Javascript frameworks like JQuery which is very effective solution for you. you deal only a few lines of JS code and you don't need exaggerate the html output (i guess it's probably long enough). about jquery there is a good source named visual jquery if you have never used that.
here is the way how to do that.
you dont need to edit inside the loop. you just only put an id to your table tag.
then add new column to your table with checkboxes inside.
then you can get the values of checkboxes & serialize them in to a hidden input. or you can handle selected rows with ajax easy. i think JS framework will work better.
normally i've added many links to post but it says it's not allowed for new users.
Related
I have like 6 tables in a page and I want to be able to read that page and pull all the tables
everything in between
I guess I just need to do this 1 time and add a while to do it until the end of the page.
I know how to use the data from a variable later or use it from an array
but I have no clue on how to attempt to grab the data from my webpage
I am going to build a page from that data with a different style but re-using the same tables
Well, using the following requires you to "know" those tables having class="class1" attribute, which you should :)
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("path/to/filename.html");
$tables = $doc->getElementsByTagName("table");
$validClasses = array("class1", "class2", "class3", "class4", "class5", "class6");
foreach ($tables as $table) {
if(in_array($table->getAttribute("class"), $validClasses)) {
// Here begins your journey :)
}
}
?>
*Now that I understand what you're after - someOne's answer seems a better starting point.
PHP can't 'pull' the data -- javascript can. If you are looking to use the data on the server side with PHP, you will need to send it to that page through POST or GET.
There are a few ways to do that, but perhaps the easiest is to set up a PHP loop on the data page that sets an input inside each field that you wish to send to your php script.
I might do it sorta like this in pseudo code -
echo <form method=post action="phpreader.php">
for ($x = 0 ; $ < $tables; $x++){
echo <table>
echo <tr><td> $data <input type=hidden name='data'.$x value = $data> </td></tr>
echo </table>
}
echo </form>
In the loop, you can put in as many table elements as you need and then attach the data to a form.
This will display your data programatically and also set it up to be send for post at the same time. You will need something to trigger this to go to post. Could be a Submit button, or javascript.
This could work, but I would maybe re-think your program method. If you are displaying this data to begin with, it has to come from somewhere. Might be a better way forward to pull the data from a database, display in the tables, and then you can re-pull the same data using the same query on your phpreader page.
I have a text box which extracts the content of dropdownlist.Now whenever i extract the content i too need to edit it ana save it into the database.How can i do????
Here is my code:
<?php
require'conn.php';
$select_query="Select dynamictext from tbl_content where type=1";
$select_query_run =mysql_query($select_query);
echo'Dynamictext:';
echo "<select name='dynamic text' id='names' >";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
$value=$select_query_array["dynamictext"];
echo "<option value='$value' >".htmlspecialchars($select_query_array["dynamictext"])."</option>";
}
echo "</select>";
?>
Based on the clarification I got above from #krisha above, I'm going to take a stab at answering this. You'll want to refer to my comment above, for a definition of (Option A) and (Option B), as I defined them.
Let's assume you've got (Option A) working and that (as far as the select HTML element is concerned), it is functional.
Let's also assume that you know to do the following:
Place the select tag inside of a form tag.
Set the form tag's action and method values.
Place a <input type="submit" value="Submit"> inside of the form tag.
If none of the above made sense, see here.
Once you've done everything above, that will result in the value of the HTML drop-down being available to PHP after the user clicks the Submit button and the page refreshes. How the value of the drop-down is passed through the submit process will depend on the method value you pass to the form tag. I'll assume you use method="get" (which will result in the value of the drop-down appearing in the URL after the refresh). If you want more info on get versus post, see here.
Once the refresh occurs, you use PHP's $_GET[""] to retrieve the value of the HTML drop-down. In your case, you would use $_GET["dynamic text"] (since the name of your select is dynamic text). You could set this value to a variable, like so:
$value_of_select = $_GET["dynamic text"];
At this point, you have the value the user selected from the HTML drop-down. Now, push it to the database. It looks like you already understand how to pass queries to a database. The only difference in this case is that you want to do an insert or an update, not a select.
i want to set more value to checkbox,how do it? this is my code: from this code only i could get upto 3 value,why? i want to get 5 value.how do it?
$results=$watch.",".$thumbnail.",".$name.",".$description.",".$val;
<input type="checkbox" name="checkbox[]" id="checkbox[]" class="addbtn" value=<?php echo $results;?>
javascript fn:
function chkbox()
{
$('[name^=checkbox]:checked').each(function() {
var ckballvalue=($(this).val());
var latlngStrs = ckballvalue.split(",",5);
var link = latlngStrs[0];
var thumbnail = latlngStrs[1];
var name = latlngStrs[2];
var description = latlngStrs[3];
var categ = latlngStrs[4];
alert(link+thumbnail+name+description+categ);
}
Assuming your markup is correct and you're using POST, then you just grab them from the $_POST variable in PHP.
$checkedValues = $_POST['checkbox'];
This will be an array of all checked values, where 'checkbox' is the name of your input group.
When a browser posts a form containing checkboxes, it only posts anything for the checkboxes that have been checked. The ones that are unchecked will not be sent at all.
Therefore, if you have five checkboxes, but only three are checked, your PHP $_POST or $_GET array will only contain entries for those three, not the other two.
This will also means that if you're using name="checkbox[]" for all your checkboxes, then your array elements will not be numbered as you expect.
Note that this is the way HTML forms work; it's got nothing to do with PHP.
If you want to force it, you could have a hidden field on the form which mirrors the checkbox state using Javascript. This way you'll be certain you'll always get the value posted to you whether it was checked or not.
Or you could just accept the way it works. You may not get the values posted to you if they aren't checked, but their absence is sufficient, as not having those field posted allows you to know that they weren't checked.
You might find it helpful to give your checkboxes explicit array element keys -- ie rather than name="checkbox[]", use name="checkbox[3]". This way they'll always be posted into the right slot in your $_POST array.
Hope that helps.
I used the code below to remove a selected item from drop down, but when I remove one, the other item pops up. For example, if these are my options: "guns, cars, money", as I select and delete guns, cars and money remains. However, if I select cars and delete it, the deleted guns options pops up again. It is frustrating.
<?php
$opts = array("guns","knives","ammo");
$selected = array($_POST['selectMenu']);
$revisedOpts = array_diff($opts,$selected);
?>
<form method="post">
<select name='selectMenu'><?php
foreach($revisedOpts as $v) {
echo "<option>".$v."</option>";
}
?></select>
<input onclick="array_diff()" name="Collect" type="submit" value="submit" />
</form>
PHP only acts when the page is loaded, and you load the same code over and over. In order for previously deleted options to stay deleted, you need some kind of data persistence (like a database). Otherwise, you can use javascript to manipulate the select options on the client side browser. Here is a good discussion
If you must bind the action to onclick() and receive the event on the server side, then you will need to use an AJAX call. The onclick calls a separate PHP script which deletes the option and returns some kind of success message.
you want to have a look at some js code to do this. look at something like that http://www.mredkj.com/tutorials/tutorial_mixed2b.html
use jquery
jquery auto suggestion
I have this webpage. It has a page called "services.php". I have several buttons (made of classes), that belong to different "package" prices i offer.
I want the links that say "Select" to autofill a form in another page, or alternativly in a popup form in the page..
I don't really know how to explain it, but as short as possible:
When link is pressed autofill form (in this or other page) with the type of package they chose. Only text autofill
What you seem to be asking is 'loading' a page pre-filled with specific information, you can do this a number of ways, either by utilizing javascript (like jQuery for instance). Or using your PHP, make links that pass variables (say a flag or a reference to pre-fill the fields -- if you want a popup or next page, etc).
Your url would like like the following for the button that a user presses (button would be a simple http link):
http://mywebsite.com/prefill.php?user=bob&package=2
This would have the values bob as the user that requests it (you can reference an id for user info here as well), and package=2 to designate your package options.
Then on the prefill.php page, you would have something that checks for:
$user = $_GET['user'];
$package = $_GET['package'];
Hope that helps
This will populate form fields with whatever you pass to the autoFill() function. This would be a same page example.
<html>
<body>
<form>
<input type="text" id="packageDescription">
<input type="text" id="packagePrice">
</form>
<script>
function autoFill(packageDescription, packagePrice) {
document.getElementById('packageDescription').value = packageDescription;
document.getElementById('packagePrice').value = packagePrice;
}
</script>
Premium Package<br>
Platinum Package
</body>
</html>
You could do something like this:
<select id="packages">
<option value="package1">Package 1</option>
<option value="package2">Package 2</option>
</select>
Submit
When the link is clicked, the following javascript will fire off:
function submitPackage()
{
var package = $("#package").val();
window.open("http://your-site.com/some-script.php?package=" + package);
}
The above will open a pop up window to a page such as this:
http://your-site.com/some-script.php?package=package1
In some-script.php you will do something like this:
You selected the package: <b><?php echo $_GET['package'];?></b>.
Or:
<?php
//Put the packages in an array:
$packages = array();
$packages['package1'] = 'Package 1';
$packages['package2'] = 'Package 2';
//...
?>
<select id="package">
<?php foreach ($packages as $name => $text):?>
<? $selected = ($name == $_GET['package']) ? 'selected' : '';?>
<option value="<? php echo $name;?>" <?php echo $selected;?>>
<?php echo $text;?>
</option>
<? endforeach;?>
</select>
The above will auto select the package they selected in a dropdown box.
if i understood your problem, you want to fill some input fields with information when the user clicks on some links
i can think of 2 ways of doing this : either have the links point to a page like services.php?req=package1 (or any other url you want) and on that page generate the input fields with the information you need (set the default values in the fields with the ones you want), or, use javascript to change the values of the forms without changing the actual page (either via ajax or predefined values)
for javascript you can use the jQuery framework, it has a pretty extensive community of enthusiasts and plenty of examples to get you started with it.
an example for your case would be
$('#btn1').bind('click', function() {
$('#input1').val("value");
$('#input2').val("value2");
});
replace btn1 with the id of the first button or link you have, input1 with the id of the first input in your form, and value with the value you want
I just did this myself. My solution was with jQuery. Just assign an id to your link. The first ID in the code is the link id and the second is the id for the input element you want to populate.
Here is the script:
<script>
$(document).ready(function() {
$('#link_id').click(function() {
$('#input_id').val( $(this).text() ).keyup();
return false;
});
});
</script>
Hope it works!
I've ran several time into the same issue, so I had to write my own script doing this. It's called Autofiller and its pretty simple but does great job.
Here is an example
http://example.com/?autofiller=1&af=1&pof=package&package=package1
So basically it takes several parameters to init the script:
autofiller=1 - init AutoFiller
af=1 - Autofill after page is loaded
pof=package - Find the parent form element of the select with name attribute package. Works also with input form elements.
package=package1 - Will set the select element's value to package1
Hope it helps you! :)