Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/294429-sumbit-to-action-function-properly/
Share on other sites

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.

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.

 

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.