I am trying to create a php file that would scan a directory, make a list of dynamic radio buttons and have the user select one and submit it and it shall pass it into another php file that would send it off, but I am having trouble with the submit button. Was wondering if I could get some answers, been looking around the net and haven't been able to find anything.
Here is my code:
<?php
ini_set('display_errors',true);
//Scan directory (ripped from another site)
$directory = opendir("download");
while($entry = readdir($directory)) {
$dirArray[] = $entry;
}
closedir($directory);
$indexCount = count($dirArray);
sort($dirArray);
//Make the Radio Button list
$RList = "";
for($i=2; $i<count($dirArray); $i++) {
$value = $dirArray[$i];
$RList .= "<input type=\"radio\" name=\"Files\" value=".$value." />".$value."<br />";
}
//Submit button
$RButton = '<input type="button" value="Submit"
onclick="location.href=\'http://localhost/appClient/load.php?file='.$value.'\'">';
echo $RList.$RButton;
#echo $_POST["Files"];
?>
Thanks
It looks like you're passing $value to the onclick event. Since that's outside of the PHP loop, it will end up always being the last $value.
To fix this, use javascript to get the selected value from the radio and append that to the onclick redirect.
Better yet, your load.php file should just simple check the 'Files' key in your $_POST and use that, instead of using GET. That's the point of submitting the radio button, after all.
I don't see the <form> tag, try to add it because the all buttons and inputs must be within the <form>
Does your script is calling the file load.php already or the button just does not work?
Related
I am new to Php. I got a checkbox array and button(they are not <form>). Once the checkbox have checked and the user click the button. It will update the value of the session. Which display how many checkbox it clicked. My problem is the code is not really working.
It is my code:
<?php
session_start();
$add = 0;
$_SESSION["add"] = $add;
if(isset($_POST['checker']) && (isset($_POST['click']))){
if (is_array($_POST['checker'])){
foreach ($_POST['checker'] as $value) {
echo $value;
$add++;
}
}
echo "<b>You have clicked".$add." box.</b>";
echo "<tr><td><input type='checkbox' name='checker[]' value=''/></td></tr>";
}
P.S I use mysql to retrieve the row count into the table so there will be 10 checkbox appear.
when you click on button what happens? You have to use javaScript to capture the button click event or if the php code is in same file use this <form action="#"> it will load again and check if the $_POST['checker'] isset.
Looking for some help with checkbox(switch type) that I have put in PHP.
These checkbox are created dynamically with data received from a MySQL Database.
The checkbox is loaded on the page depending on the condition of the checkbox placed on the database.
Now, I need help to build a code that perform some action like writing back into another database table everytime this checkbox is actioned; i.e. its inital state can be :checked or left blank(unchecked) and each of these checkbox have an ID or value on them from database. But any action that takes place either from blank to :checked or from :checked to blank, it needs to trigger an action for that particular actioned checkbox only.
Would need help on this as PHP please.
My Code Looks like this for a checkbox:
echo "<input type='checkbox' class='input-swtoggle' name='switch-box' ";
$lightStatus = $row->light_status;
if ($lightStatus == "1") {
echo "checked>";
}
else{
echo ">";
}
PS: I did try alot Googling and on Stackoverflow, but none of them are helping for the above condition.
You can bind click event to the checkbox using .on()
$(function(){
$(document).on('click',"input:checkbox.input-swtoggle",function() {
alert($(this).is(':checked'));
});
});
<?php
// HTML code
$checkced = ($row->light_status == '1') ? 'checked="checked"' : '';
echo '<input type="checkbox" class="input-swtoggle" name="switchbox[]"' . $checkced . '/>';
// Code after form posted:
if (! empty($_POST['switchbox'])) {
foreach ($_POST['switchbox'] as $switch) {
// Apply your logic here.
}
}
?>
I might be on the wrong path (tiredness..) for the moment but i have passed 4 hours or something similar to debug my code. I have a form that is auto submitted when I click on a radio button, once clicked the next form appear and let me input the customer information. But when the page reload for displaying the other form, my variable $CustomerType is set and correct and, when i complete the input form (the second one) the php check if everything in it seems correct and it does, but it says my variable $CustomerType is missing then reload the page and ask me again to set the type.
I can't paste all my code out here (~300 lines) but here is the core :
<?php $_POST['CustomerType']="SOMEONE"; ?> // Ok so this was the trick, it solved the main bug but it now fix my choice to SOMEONE only. Can't change to another type
<form method="post" action="<?php echo escaped_html($_SERVER['PHP_SELF'],'URL');?>">
<?php
$array=show_enum("customer", "CustomerType");
$checked="";
foreach($array as $CustomerType)
{
$checked="";
if(isset($_POST['CustomerType']))
{
if($_POST['CustomerType']==$CustomerType) $checked="checked";
}
echo "<input type='radio' name='CustomerType' value=".$CustomerType." ".$checked." onClick='this.form.submit()'>".$CustomerType."</input>";
}
?> </form>
EDIT Ok there is some news : by modifying the top who was : <?php $_POST['CustomerType']="SOMEONE"; ?>
TO
if(!isset($_POST['CustomerType'])) $_POST['CustomerType']="SOMEONE";
It seems to solve the second problem of the form, which couldn't let me change the type (auto-rollback to SOMEONE). But now, on form submit my choices are always rolling back to [CustomerType] => SOMEONE instead of SOMEBODY (and i checked SOMEBODY).
It means that i can't hold the value $_POST['CustomerType'] on page reload for submitting.
For example : This one which seems identical except that it's submitted with "save" button instead of onsubmit is working fine.
$array=show_enum("customer", "Language");
foreach($array as $Language)
{
$checked="";
if(isset($_POST['Language']))
{
if($_POST['Language']==$Language) $checked="checked";
}
else if($Language=="FR") $checked="checked";
echo "<input type='radio' name='Language' value=".$Language." ".$checked." />";
string2languagepic($Language);
}
Picture of the problem *OnSubmit = onClick='this.form.submit()
After looking at your code a little more I think I have spotted your problem, try the following and see if it works.
echo "<input type='radio' name='CustomerType' id='CustomerType' value='$CustomerType' $checked onClick='this.form.submit();' >"
If that fails you could always add a hidden field, and when clicking on the radio button it adds a value to it and then submits the 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.
i have 3 pages
Page1.php,page2.php,page3.php
In page1.php, i have some hidden values, for example 'name'
After the submission of page1.php, it will go to page2.
Then after some process in page2.php, it should need to automatically submit to page3.php(where page3.php is in another sever)
Finally,when i print the $_POST variables in page3.php, i need to get the variable 'name'
you could stick it in the session
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
or you could pass them in hidden vars on page2.php if it has a form...
You will want to look into sessions.
If you need them in POST, try this:
$display = "";
$saveFields = array('one', 'two'); // whitelist of fields to add to the form hidden
foreach ($_POST as $key => $val) {
if (!empty($val) && in_array($key, $saveFields))
$display .= '<input type="hidden" name="'.$key.'" value="'.$val.'" />';
}
echo $display;
Should get you where you want to go. The whitelist just ensure's that random stuff is not injected that does not need to be.
(1) Option is to add hidden input on page2 too.
(2) Option is to set the value from page1's name into session and use it on page3
There are several solutions:
PHP sessions
cookies
passing arguments as GET/POST parameters
storing data in database
In simple cases passing arguments as GET parameter page2.php?name=... or using hidden form field is the best solution
This seems straightforward to me, page one has a hidden value called name. Page 2 should retrieve the post $_POST['name'] and print it on page 2 as a hidden field. Once you post it to Page 3 you can retrieve it the same way $_POST['name'].
Realistically if the data is exactly the same and is being carried all the way to page 3, why do you even need it? Can you not just declare it on page 3?
Okay, the way I read this is that on your first page you have a UI with a form. The form is then submitted for processing to page 2. After processing is done, you want to redirect, if you will, the user to another site (or server, doesn't necessarily have to make a difference).
If I got that right, here is what you should do; instead of using the header(); function (php), print an empty page with a hidden form with all of the details you want to send over and use javascript to emulate the user 'submitting' the form.
< div style="display: none;">
< form action="https://mywebpage.com/myscript.php" method=POST>
< input type=hidden name="key_1" value="value_1">
< input type=hidden name="key_2" value="value_2">
< input type=hidden name="key_3" value="value_3">
< input type=submit id="formButton" style="visibility: hidden; ">
< script language="javascript">
document.getElementById("formButton").click()
< /form>
< /div>