I'm trying to cycle through my photo gallery with next / previous buttons. When the user clicks on a photograph it enlarges it. When they click next it should go to the next photo. When the user clicks on the submit button immediately after selecting the photo it will enlarge the next photo. However when they click submit (next) again it doesn't update the $index variable. I know the reason is because once its set the first time the isset($_POST['myNext') evaluates to false. So how can I update $index each time they click on the button. I don't want to use javascript for this because it should be possible with PHP. I also don't want to just copy and paste some JS i don't understand. I've looked online for answers but have found nothing without JS. I've omitted the code to scan the directory with the images. Thank you.
<?php
for($i = 0; $i < count($thumbArray); $i++)
{
$currentIndex = $thumbArray[$i];
echo '<div class = "img"> <img src="'.$dir.$thumbArray[$i].'" alt="Picture" height = "100" width = "100" /> </div>';
}
if(isset($_GET['original_selection']))
{
$original_selection = $_GET['original_selection'];
}
for($i = 0; $i < count($thumbArray); $i++)
{
if($thumbArray[$i] == $original_selection)
$index = $i;
}
?>
<html>
<head><title>Single</title></head>
<body>
<?php
if(isset($_POST['myNext']))
{
echo 'index: '.$index;
$index++;
}
?>
<div class = "img"> <img src="<?= $dir.$thumbArray[$index] ?>" alt = "Picture" height = "500" width = "500" /></div>
<form name="NextForm" method="post">
<br />
<input type="submit" name="myNext" value="Next"/>
</form>
<h1>hi</h1>
</body>
</html>
Have you tried to output what $index is before the if(isset($_POST['myNext']))?
You miss the action parameter in the form tag:
<form name="NextForm" method="post" action="">
^ Doing that will send the form data to the same page.
This seems like very inefficient code I'm allowed to be brutally honest.
for($i = 0; $i < count($thumbArray); $i++)
{
if($thumbArray[$i] == $original_selection)
$index = $i;
}
Why are you searching like that? Not only will it continue to search after it finds it's value, but there are already a php function for this, namely array_search. Also you don't need to loop like that in php, just use the foreach loop. If you really had to do what you tried above, you should have used "break;" inside the if.
Instead of this:
echo 'index: '.$index;
$index++;
You could have just done this:
echo 'index: '.$index++;
EDIT:
Where do you store the thumbarray? This could all have been made much easier and more efficient if you used a database to store the name of the files and gave them for example a group id number, an id etc.
Related
I am trying to get a grasp on how to properly understand how POST works and decided to make a little PHP program that generates 100 checkboxes, and at the click of the button on the top of the document, a function is called that loops through all of the checkboxes and states whether or not they are checked. However, it seems like the way I am implementing it does not work.
Here is a more sequential step by step process of what I was thinking as I was writing this script, I would appreciate if someone could tell me the flaw in my thinking.
Create a button in a form with method=post.
Create an array in PHP to store all the checkbox names.
Create a for loop. In each iteration of the loop, a string is created with the format of #checkbox so each checkbox has its own unique identifier. Each one of these strings is stored in an array. PHP then echo's out a form with method=post with a checkbox inside each form. Each checkbox has its unique name.
After each checkbox is created, you can see if each checkbox is checked or not by clicking the button, which calls the checkFunction() function.
In the function, there is a foreach loop. This loop goes through each element in the array to fetch the checkbox name, uses isset to see if the checkbox is checked or not, and echo's out if it is.
This seems simple enough to me, but it doesn't seem to work for whatever reason. Could anyone shed some light as to why not?
<!DOCTYPE html>
<html>
<head>
<title>Checked Boxes</title>
</head>
<body>
<form method="post">
<input type="submit" name="submit_checked" class="button" value="Check if checkbox is checked"/>
</form>
<?php
$checkboxNames = array();
for ($x = 1; $x <= 100; $x++) {
$checkboxStr=$x."checkbox";
$checkboxNames[] = $checkboxStr;
echo "<form method=\"post\"><input type=\"checkbox\" name=\"$checkboxStr\" value=\"$checkboxStr\">$checkboxStr</form>";
}
echo("<hr>");
echo("<h2>Status of Checkboxes:</h2>");
function checkFunction(){
foreach ($checkboxNames as $currentName){
if (isset($_POST["$currentName"]))
echo("<p>$currentName is checked!</p>");
else
echo("<p>$currentName is not checked!</p>");
}
}
if(isset($_POST['submit_checked']))
checkFunction();
?>
</body>
</html>
You need to remove form tag in foreach, you should only use 1 <form method="post"> in a form. And put checkbox inputs inside your main form;
PHP function cant access variable outside that function, so you should put that variable in params.
Below is the working code:
<!DOCTYPE html>
<html>
<head>
<title>Checked Boxes</title>
</head>
<body>
<form method="post">
<input type="submit" name="submit_checked" class="button" value="Check if checkbox is
checked"/><br>
<?php
$checkboxNames = array();
for ($x = 1; $x <= 100; $x++) {
$checkboxStr = $x."checkbox";
$checkboxNames[] = $checkboxStr;
echo "<input type=\"checkbox\" name=\"$checkboxStr\" value=\"$checkboxStr\">$checkboxStr <br>";
}
echo("<hr>");
echo("<h2>Status of Checkboxes:</h2>");
function checkFunction($checkboxNames)
{
foreach ($checkboxNames as $currentName) {
if (isset($_POST[$currentName])) {
echo("<p>$currentName is checked!</p>");
} else {
echo("<p>$currentName is not checked!</p>");
}
}
}
if (isset($_POST['submit_checked'])) {
checkFunction($checkboxNames);
}
?>
</form>
</body>
</html>
You are submitting the first form only while all your checkboxes are in separate forms that are not being submitted. Try to rewrite your loop to have checkbox inputs inside your main form.
I am trying to access different elements in an XML so I can edit them or add new ones from within a web page. I'm using PHP to loop through the parent tags and display in a select list then have submit button to POST the element selected - no issue.
I then use PHP to find all the names of the selected elements children, and display those names in another select list form. This displays the children in the list correctly however when I hit submit it displays correctly if I echo $_POST("Section") but clears the previous $_POST("Page").
I think this has something to do with using action="", but interestingly if I change one them to GET it works as intended. I actually want a 3rd step into lower children again, so cannot use that solution as a dodgy workaround.
I wont post XML as it has sensitive data in it and you can just trust I am stepping through that fine, it's the php clearing $_POST that is the big issue for me.
Please go easy - I literally learnt how to make a basic html webpage in april.
Tried a bunch of different form action including the .php it is on, and also
<?php echo $_SERVER['PHP_SELF']; ?>" >
As stated before - using one as GET and one POST seems to solve the issue
<?php
//pages processing and put into an array
$pages_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
array_push($pages_array, $page->name);
}
$pagecount = count($pages_array);
?>
<form name="page_select" method="POST" action="">
<select name="Page">
<?php
for ($i = 0; $i < $pagecount; $i++){
print '<option>'.$pages_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_page" value="edit"/>
</form>
<br></br>
<?php
// save page selected into $pg and display page being edited
$pg = $_POST["Page"];
echo 'YOU ARE EDITTING PAGE: '.$pg;
?>
****** THEN FURTHER DOWN THE PAGE *****
<?php
// section processing
$section_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
if($page->name == $pg){
foreach($page->sections->section as $section){
array_push($section_array, $section->name);
}
}
}
$sectioncount = count($section_array);
?>
<form name="section_select" method="POST" action="">
<select name="Section">
<?php
for ($i = 0; $i < $sectioncount; $i++){
print '<option>'.$section_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_section" value="edit"/>
</form>
<br></br>
<?php
$sct = $_POST["Section"];
echo 'YOU ARE EDITTING SECTION: '.$sct;
?>
I want output to remember both arrays after the second form is submitted, however due to the first POST variable being wiped it means I also lose the second array since when it gets back to pushing to $section_array $page->name can never = $pg since $pg is now empty
Every time you submit a form, the page is reloaded and PHP will only have access to the POST variables from the form that triggered the page reload. This means that as your second form is submitted, $_POST['Page'] ceases to exist.
There are many ways around this problem, here is a fairly simple one. In your second form, create a hidden input that contains the value of the $_POST['Page'] variable. That way, when the second form is submitted, the Page value will be propagated to the next step of the process. Adding this code to your section_select form should suffice:
<?php
if(isset($_POST['Page'])) {
?>
Page: <?php echo $_POST['Page'] ?>
<input type="hidden" name="Page" value="<?php $_POST['Page'] ?>" />
<?php
}
?>
How can I get the value of the amp selector and save it in a php variable
<amp-selector multiple layout="container" class="radio-selector"
on="select: AMP.setState({
selectedOption: event.targetOption,
allSelectedOptions: event.selectedOptions
})">
<div option="1">1</div>
<div option="1">1</div>
<div option="1">1</div>
</amp-selector>
<input type="text" name="" [value]="allSelectedOptions.join(' ')">
<?php
$category_id ='"selectedOption"';
// $category_id ='{{selectedOption}}';
// $category_id=remove_special_characters($category_id);
$int = (int) filter_var($category_id, FILTER_SANITIZE_NUMBER_INT);
echo $int;
$filter= get_filter_category($category_id);
?>
I tried multiple ways like domdocument but nothing seems to work.
Any help would be appreciated
$dom = new DOMDocument();
$dom->loadHTML($html);
//$num = "";
$optionTags = $dom->getElementsByTagName('div');
print_r($optionTags);
for ($i = 0; $i < $optionTags->length; $i++ ) {
if ($optionTags->item($i)->hasAttribute('selected')
) {
$num = $optionTags->item($i)->nodeValue;
}
}
echo "Num is " . $num;
Normally you would need this for a form.
Hence you will have a submit button or something similar.
Way to go here is to link your submit button to another file that reads the selection (possible via POST) and redirects you to the desired page.
If you just want to save your selection without doing anything, you could use amp-bind and a JSON Endpoint. The Endpoint gets called when changed, with the selection as parameter and saves it instead of delivering any files what so ever.
I'm trying to create a repeatable field (an upload image input with image preview) in PHP using jQuery.clone(). Everything works fine, except the return of the clone data.
In my PHP file, I have:
$i = 0;
$valid_input['image'] = $input['image'][$i];
then
return $valid_input;
used in an image upload input:
<input type="hidden" class="image" name="image[image]['.$i.']" value="'.$theme_options['image'].'" />
<input type="button" class="upload-button button" value="'. __( 'Upload Image', 'theme' ).'" />
the value of $i is set to 0 and counted with jQuery clone.
The problem is that the cloned fields disappear after submitting.
The original field "image['image'][0]" is saved and returns as valid, but the others (image['image'][1], [2], [3]...) don't validate!
If I change the value of $i like this:
$i = 1;
$valid_input['image'] = $input['image'][$i];
then the original input don't submit, only the clone, because the original is [0] and the clone is [1], but after submitting the clone returns as [0].
I tried things like:
$i = 0;
$valid_input['image'] = $input['image'][$i];
$i++; //-- I know, I'm stupid...this will not count the input!
Please, can somebody help me with this?
How can I be able to validate the cloned fields?
My Google searches are all marked as visited and I swear that I did not find anything that could solve this!
Any help would be greatly appreciated, thanks in advance!
Do you mean something like:
for($i = 0; $i < count( $input["image"] ); $i++) {
echo $input["image"][$i];
}
or did i get you wrong
please try with this
using the input name as image[] so you will get the value of image as an simple array by using the foreach you can do you validation.
i.e.,
$arrValue = $_POST['image'];
foreach ($arrValue as $key=>$value) {
//do the operations here
}
I am having issues trying to access a submitted image from a form on the next page.
Here is my php in my html.
I am looping through a folder of images and echoing them onto the page.
<form action = "Order_Form.php"
method = "post"
enctype = "multipart/form-data">
<?php
$files = glob("images/*.*");
$count = 0;
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
if($count == 4){
echo "<br>";
$count = 0;
}
$count++;
echo '<input type="image" src="'.$num.'"
alt="img" name="image" class = "galImgs" value="submit" />';
}
?>
</form>
And now on submit I proceed to Order_Form.php
I want to display the picture the user clicked/submitted to the page.
I have been trying with
$nm = $_POST['image'];
echo '<img src="/files/images/.$nm" class= "image"/>';
Im pretty sure it has to be something simple, but after much googling and trial & error I can't seem to figure this out.
Any help would be appreciated. Thank you.
The value of your button will be that of it's value attribute, or always Submit.
Use an array of images in the $_POST by appending [$num] to the input name attribute.
echo '<input type="image" src="'.$num.'"
alt="img" name="image[' . $num . ']" class = "galImgs" value="submit" />';
This will send the input image to $_POST as an array which looks like
// Suppose two of them (22 & 14) were clicked (which isn't actually possible but illustrates it)
// The values are always "submit" but the keys are useful to you.
array(1) {
["image"]=>
array(2) {
[22]=>
string(6) "submit"
[14]=>
string(6) "submit"
}
}
Then when you retrieve it from $_POST, you know which has been clicked:
foreach ($_POST['image'] as $im=>$num) {
// The array key holds the image number
echo '<img src="/files/images/' .$im .'" class= "image"/>';
}
Your $nm variable it not being interpretted because it is in single quotes.
You will probably have an easier time if you simply use normal <img> tags with an onClick event that sets a hidden input field to the value of the selected image.