chrathan Posted February 6, 2015 Share Posted February 6, 2015 I am reading images from the directory 'images' which contains several folders with images. I want to show every image in a div and with the checkbox to decide if it is fashion or not fashion. I am using the following code in order to read files from directory and showing them in browser. The divs is one after other: <?php function listFolderFiles($dir){ $html = ''; // Init the session if it did not start yet if(session_id() == '') session_start(); $html .= '<ol>'; foreach(new DirectoryIterator($dir) as $folder){ if(!$folder->isDot()){ if($folder->isDir()){ // $html .= '<li>'; $user = $dir . '/' . $folder; foreach (new DirectoryIterator($user) as $file) { if($file != '.' && $file != '..'){ $image = $user . '/' . $file; // Check the pictures URLs in the session if(isset($_SESSION['pictures']) && is_array($_SESSION['pictures'])){ // Check if this image was already served if(in_array($image, $_SESSION['pictures'])){ // Skip this image continue; } } else { // Create the session variable $_SESSION['pictures'] = array(); } // Add this image URL to the session $_SESSION['pictures'][] = $image; // Build the form //echo $image, "</br>"; $html .= ' <style> form{ font-size: 150%; font-family: "Courier New", Monospace; display: inline-block; align: middle; text-align: center; font-weight: bold; } </style> <form class = "form" action="' . action($image) . '" method="post"> <div> <img src="' . $image . '" alt="" /> </div> <label for="C1">FASHION</label> <input id="fashion" type="radio" name="fashion" id="C1" value="fashion" /> <label for="C2"> NON FASHION </label> <input id="nfashion" type="radio" name="nfashion" id="C2" value="nfashion" /> <input type="submit" value="submit" /> </form>'; // Show one image at a time // Break the loop break 2; } } // $html .= '</li>'; } } } $html .= '</ol>'; return $html; } //##### Action function function action($img){ $myfile = fopen("annotation.txt", "a"); if (isset($_POST['fashion'])) { fwrite($myfile, $img." -- fashion\n"); // echo '<br />' . 'fashion image'; } else{ fwrite($myfile, $img." -- nonFashion\n"); // echo '<br />' . ' The submit button was pressed<br />'; } } echo listFolderFiles('images//'); ?> When I make call of action($image) from inside form tag, I noticed, that a default value of submit it is parsed to the function, thus it stores the name of the image with the default value and the chosen value for the first image it is parsed to the second image, the chosen value of the second image it is stored to the file with the third image and so on. When I make call in side input submit tag it began storing to the file from the second image with the value of the first one and so on. I couldn't manage to have stored to the file the correct correspondence between the image and the chosen value. Where should I make call of the action function in order to do it properly? Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/ Share on other sites More sharing options...
Ch0cu3r Posted February 6, 2015 Share Posted February 6, 2015 This line of code is not doing what you think it is doing <form class = "form" action="' . action($image) . '" method="post"> The action function will not be called when the form has been submitted. PHP will be calling that function immediately! PHP cannot react to events happening in the browser. PHP code will only be executed when a HTTP request takes place. What you need to do is only call the action function when the form has been submitted. Example code if($_SERVER['REQUEST_METHOD'] == 'POST') { // call the action function passing the name of the image action($_POST['image']); } You will now need to add a hidden input field to your form containing the name of the image. Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505075 Share on other sites More sharing options...
chrathan Posted February 6, 2015 Author Share Posted February 6, 2015 I understand the problem, I am not sure if I understand the solution. I am not that familiar with php, so is it possible to clarify your answer? Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505078 Share on other sites More sharing options...
chrathan Posted February 9, 2015 Author Share Posted February 9, 2015 Can I add the code in a new php file using the following: if(isset($_POST['submit'])) { display(); //function action } Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505224 Share on other sites More sharing options...
chrathan Posted February 10, 2015 Author Share Posted February 10, 2015 I tried to use: if($_SERVER['REQUEST_METHOD'] == 'POST'){ action($image); } Before html form, the results was as in the case I place my function inside <input type="submit" value="submit" /> . It store my choice with the next image in listfile neglecting the first image. Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505320 Share on other sites More sharing options...
Ch0cu3r Posted February 10, 2015 Share Posted February 10, 2015 I understand the problem, I am not sure if I understand the solution. I am not that familiar with php, so is it possible to clarify your answer? Sorry I didn't reply to you earlier. You need to add the name of the file as a hidden input field because your action function takes the image path as an argument. Without this PHP will not know what file you are referring to when calling the action function. With my solution you first define the name of the image as a hidden input field called image by changing your forms code to the following <form class="form" action="" method="post"> <div> <img src="' . $image . '" alt="" /> </div> <label for="C1">FASHION</label> <input id="fashion" type="radio" name="fashion" id="C1" value="fashion" /> <label for="C2"> NON FASHION </label> <input id="nfashion" type="radio" name="nfashion" id="C2" value="nfashion" /> <input type="hidden" name="image" value="'.$image.'" /> <input type="submit" value="submit" /> </form>' Notice I have set the forms action to be empty, this so the form will be submitted to itself. The action only needs to be set if you are going to redirect the user to a different page. The other addition is the hidden input field called image before the submit button. This is set to the value of $image. To call your action function when the form has been submitted change echo listFolderFiles('images//'); to if($_SERVER['REQUEST_METHOD'] == 'POST') { // call the action function when a post request has been made. action($_POST['image']); } echo listFolderFiles('images//'); Now the action function will be called when you have submitted a form and the name of the file will passed to the function. Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505361 Share on other sites More sharing options...
chrathan Posted February 12, 2015 Author Share Posted February 12, 2015 Thanks Ch0cu3r , it was something I was not aware of. Quote Link to comment https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/#findComment-1505505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.