Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Keep echo after submit button

    Hello,

    I have in my app multiple submit buttons, each one execute a different condition and as a result, display an image.

    Problem I have : When I click on one submit button, its image will display correctly. But if I click on a second submit button, the image of the first submit button will disappear.
    I would like it to stay, until I click on this button again.

    Any ideas how I could do this ? Thank you.

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    Web servers are stateless. They don't know what happened outside of the current request.

    You would need to propagate the data between requests, either by passing the values in the url, hidden form fields, session variables, cookies, or in a database table. The best method depends on what you are actually doing.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. #3
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The user check the checkboxes he wants and press the submit button. It will compare the value of the checkboxes with others values in the db. Depends on the result, an image will be displayed on another page.

    I tried session for now but still the same problem.

    PHP Code:
    $_SESSION['submit'] = $_POST['submit'];

    if(isset(
    $_SESSION['submit'])){
    echo 
    "hey";


  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,378
    Thanks
    10
    Thanked 1,190 Times in 1,180 Posts
    Don't use "submit buttons", as in a <form>
    Use PHP with JQuery (or AJAX).
    PHP scripts are executed, your images are changed, and the screen does not need to refresh.

    You'll also need to post more of your script, and maybe an example of what you're doing so we can see it.

    As CFM said, "The best method depends on what you are actually doing". ... or what you're trying to do.

  5. #5
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not sure if I need AJAX for now. If my page is refresh after submit the button, it's fine. I just need them to all display after I click on all submit buttons.

    Here is the part of my code : (All this code is inside a loop foreach as $host)

    PHP Code:
    <?php
    echo '<input type="submit" name="'.$host.'" value="test"> <br>';                   
                        
    if(isset(
    $_POST[$host])){                    //submit
        
    if(!empty($_POST[$radio])){             //radio         
            
    $manual $_POST[$radio]; 

            if(
    $manual == 'Green') echo '<img src="Green.php" alt="">'."<br>";
            if(
    $manual == 'Yellow') echo '<img src="Yellow.php" alt="">'."<br>";
            if(
    $manual == 'Red') echo '<img src="Red.php" alt="">'."<br>";                 
        }

        if(empty(
    $_POST[$radio])){ 
            
    $ids $_POST['list'];                   //checkboxes
            
    $idQue implode(", ",array_fill(0,count($ids),"?"));

            
    $query "SELECT servicestatus_id,current_state FROM nagios_servicestatus WHERE servicestatus_id IN (".$idQue.")";
            
    $select $db->prepare($query);
            
    $select->execute($ids);
            
    $results $select->fetchall();
                                
            
    $containsOne false;
            
    $containsTwo false;
            
    // first loop over all results to check the content of the array
            
    foreach($results as $row){
                if (
    $row['current_state'] == 1){
                    
    $containsOne true;
                }              
                if(
    $row['current_state'] == 2){
                    
    $containsTwo true;
                }
            }    
            
    // once the loop is done, check the results and echo the correct message
            
    if (!$containsOne && ! $containsTwo) echo '<img src="Green.php" alt="">'.'<br>';
            if (
    $containsOne  && ! $containsTwo) echo '<img src="Yellow.php" alt="">'.'<br>';
            if (
    $containsTwo) echo '<img src="Red.php" alt="">'.'<br>';
        }
    }
    ?>

  6. #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    What you need to do is make the form fields 'sticky', by (re)populating them with any submitted values. This will require that the form field names be unique and tied to the $host value (the 'list' field of checkboxes is currently not.) The easiest way of doing this is to make the field names arrays and use the $host value as the main array index. In the case of radiobuttons and checkboxes, you would have a second array dimension/index to support multiple values for each $host value.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  7. #7
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Uuuuh I didn't understand you so well. I don't think I can make the checkboxes an array, they all need to have the same value in order to properly perform the condition. What change could I make without disrupting what my code does ?
    PHP Code:
    <?php
    $list 
    = ['PEP','Netapp'];
    $radios = ['PEPRadio''NetappRadio'];

    $service $db->prepare($queryservice);
    $service->fetchAll(); ?>
    <form method="POST" action="#">
       <?php 
                
        
    foreach (array_combine($list$radios) as $host => $radio)
        {        
             
    $service->execute([$host]);
             echo 
    $host."<br>";

             foreach (
    $service as $row)
             { 
                  
    //checkboxes
                  
    echo '<input type="checkbox" name="list[]" value='.$row['service_id'].'>';
                  echo 
    $row['display_name']."<br>";   
              }
    ?>  
              <?php
              
    //radio + submit
              
    echo '<input type="radio" name="'.$radio.'" value="Green"/>Green 
              <input type="radio" name="'
    .$radio.'" value="Yellow"/>Yellow
              <input type="radio" name="'
    .$radio.'" value="Red"/>Red
                            
              <input type="submit" name="'
    .$host.'" value="test"> <br>';
              
    ?>;
    Thanks for help.

  8. #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    Now that you have posted more of your code, so that we have an idea of what you are trying to do, some of this makes no sense.

    1) The $service->fetchAll(); line is in the wrong location so the current checkbox code doesn't work at all. This line would need to be after the $service->execute([$host]); line for the code to work.

    2) The 'manual' radio buttons, either just displaying the green/yellow/red radio button choice or causing the database values to be used to display the green/yellow/red result 'seems' like you may be trying to get to the point of displaying the current database information and 'editing' it? If you are actually going to be editing the data in the database table, don't sneak up on the problem. You will waste a bunch of time writing code that doesn't have anything to do with the task.

    I'll have to get back to you on what you would change in the code, since deciphering what the logic is trying to do will take a little while.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  9. #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    It will take longer to try and explain what you need to do then what it would take to write the code. Here's example code that does what I think you are currently tying to do -

    PHP Code:
    <?php
    // display the submitted form data
    echo '<pre>'print_r($_POST); echo '</pre>';

    $list = ['PEP','Netapp'];

    // define the radio button choices so that they can be dynamically produced. makes for less logic and you can make them 'sticky'
    $radio_choices = ['Green','Yellow','Red'];

    $service $db->prepare($queryservice);

    // this line needs to be after the execute() call $service->fetchAll();
    ?>
    <form method="POST" action="#">
    <?php
    foreach ($list as $host// host is 'PEP','Netapp'
    {
        
    $service->execute([$host]);  // gets id and name for the services for the current host value
        
    $service->fetchAll();
        echo 
    $host."<br>";

        
    // output checkboxes
        
    foreach ($service as $row)
        {
            
    $chk = isset($_POST["list"][$host][$row['service_id']]) ? ' checked' '';
            echo 
    "<input type='checkbox' name='list[$host][{$row['service_id']}]'$chk>";
            echo 
    $row['display_name']."<br>";
        }

        
    // output radio buttons + submit
        
    foreach($radio_choices as $choice)
        {
            
    $chk = isset($_POST["radio"][$host]) && $_POST["radio"][$host] == $choice ' checked' '';
            echo 
    "<input type='radio' name='radio[$host]' value='$choice'$chk/>$choice";
        }

        
    // if all you are doing is displaying information, the submit button doesn't need to be named, since the form processing code will operate on any submitted data
        
    echo "<input type='submit' value='test'> <br>";

        
        
    // display the output based on any radio button choice
        
    if(!empty($_POST["radio"][$host]))
        {
            
    $manual $_POST["radio"][$host];
            
    // you should dynamically produce this output by looping over the $radio_choices array
            
    if($manual == 'Green') echo '<img src="Green.php" alt="">'."<br>";
            if(
    $manual == 'Yellow') echo '<img src="Yellow.php" alt="">'."<br>";
            if(
    $manual == 'Red') echo '<img src="Red.php" alt="">'."<br>";
        }

        
    // if there is no radio button selected, display the output based on the any checkbox choices
        
    if(empty($_POST["radio"][$host]) && !empty($_POST["list"][$host])){
            
    $ids array_keys($_POST["list"][$host]);
            
    $idQue implode(", ",array_fill(0,count($ids),"?"));

            
    $query "SELECT servicestatus_id,current_state FROM nagios_servicestatus WHERE servicestatus_id IN (".$idQue.")";
            
    $select $db->prepare($query);
            
    $select->execute($ids);
            
    $results $select->fetchall();

            
    $containsOne false;
            
    $containsTwo false;
            
    // first loop over all results to check the content of the array
            
    foreach($results as $row){
                if (
    $row['current_state'] == 1){
                    
    $containsOne true;
                }
                if(
    $row['current_state'] == 2){
                    
    $containsTwo true;
                }
            }
            
    // once the loop is done, check the results and echo the correct message
            
    if (!$containsOne && ! $containsTwo) echo '<img src="Green.php" alt="">'.'<br>';
            if (
    $containsOne  && ! $containsTwo) echo '<img src="Yellow.php" alt="">'.'<br>';
            if (
    $containsTwo) echo '<img src="Red.php" alt="">'.'<br>';
        }
    }
    ?>
    </form>
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  10. Users who have thanked CFMaBiSmAd for this post:

    xarj (Mar 1st, 2017)

  11. #10
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey,

    Wow thank you for your great help, I didn't expected that. You should've asked me to explain what my code do, it would have save you time haha.
    I know my code is probably poorly writed, but it does what I want for now.

    It's not my final code, it's just the "basic" and I need to add more features.

    The code you writed is almost good, except a few things :
    • Every checkboxes need to have the same name, because I need to be able to check and compare multiple checkboxes from different host. For example : I check 2 checkboxes from PEP, 2 checkboxes from Netapp, and click on the submit button. My code will compare these 4 checkboxes, compare them, and display the result image. I tried to edit your line with the checkboxes but it doesn't work. I also can't find where is your code to keep the image display, as well as the checkboxes.
    • The radios button don't need to edit the database, they just need to display. The way you did is perfect, except that I can't uncheck the radios. This mean if the radio is selected one time, I can never use the checkboxes comparison later.


    1. What my code does now: It will look if there's radio checked, if yes, it will display the radio's result. If not, it will look all the checkboxes check, and display the result.
    2. What I need to do : the user will enter a text in a text field, check the checkboxes he wants, press a submit button and the image will display. The user can do this x times he wants.
      Also, the result need to be displayed on another page, but I don't think this is more difficult than just add SESSION.

    I don't think the code will change much, I just need to insert a text field with one submit button before the code I already wrote.

    Questions :
    1)Shoud I use my code or your code ?
    2)For the feature I want to add, should I just loop the text field or loop the <form> ?

    Thanks for you help!
    Last edited by xarj; Mar 1st, 2017 at 01:00 PM.

  12. #11
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey, I edited my code with what my final app should do and using some of your lines aswell.

    What my code do now : On the config page, the user fill an input text, check the checkboxes or radio as before, click submit button. The result will display on the page, as well as on another page (display's page).

    PHP Code:
    <?php
    $radio_choices 
    = ['Green','Yellow','Red'];
                
    $queryhost "SELECT display_name FROM nagios_hosts";
    $host $db->prepare($queryhost);
    $host->fetchAll();
    $host->execute();

     
    $queryservice 
                
    "SELECT display_name, service_object_id FROM nagios_services WHERE host_object_id = 
                (SELECT host_object_id FROM nagios_hosts WHERE display_name = ? LIMIT 1)"
    ;
                
    $service $db->prepare($queryservice);
                
    $service->fetchAll(); ?>
            
                <form method="POST" action="#">
                    
                    <input type="text" name="something" value="<?= isset($_POST['something']) ? htmlspecialchars($_POST['something']) : '' ?>"/> <br>
                    <?php
                    
    foreach($radio_choices as $choice)
                    {
                        
    $chk = isset($_POST["radio"]) && $_POST["radio"] == $choice ' checked' '';
                        echo 
    "<input type='radio' name='radio' value='$choice'$chk/>$choice";
                    }
                    
    ?>
                    <input type="submit" name="submit" value="test"> <br>';
                    
                    <?php    
    ///display
                            
    if(isset($_POST['submit'])){
                                echo 
    htmlspecialchars($_POST['something']);
                                echo 
    "<br>";
                                
                                if(!empty(
    $_POST["radio"])){                      
                                    
    $manuel $_POST["radio"]; 

                                    if(
    $manuel == 'Vert') echo '<img src="Green.php" alt="">'."<br>";
                                    if(
    $manuel == 'Orange') echo '<img src="Yellow.php" alt="">'."<br>";
                                    if(
    $manuel == 'Rouge') echo '<img src="Red.php" alt="">'."<br>";                 
                                }

                            if(empty(
    $_POST["radio"])){ 
                                
    $ids $_POST['list'];
                                
    $idQue implode(", ",array_fill(0,count($ids),"?"));

                                
    $query "SELECT servicestatus_id,current_state,service_object_id FROM nagios_servicestatus WHERE service_object_id IN (".$idQue.")";
                                
    $select $db->prepare($query);
                                
    $select->execute($ids);
                                
    $results $select->fetchall();

                                foreach (
    $results as $row){
                                    echo 
    $row['servicestatus_id']. "<br>";
                                }
                                
                                
    $containsOne false;
                                
    $containsTwo false;
                                
    // first loop over all results to check the content of the array
                                
    foreach($results as $row){
                                    if (
    $row['current_state'] == 1){
                                        
    $containsOne true;
                                    }              
                                    if(
    $row['current_state'] == 2){
                                        
    $containsTwo true;
                                    }
                                }    
                                    
    // once the loop is done, check the results and echo the correct message
                                
    if (!$containsOne && ! $containsTwo) echo '<img src="Green.php" alt="">'.'<br>';
                                if (
    $containsOne  && ! $containsTwo) echo '<img src="Yellow.php" alt="">'.'<br>';
                                if (
    $containsTwo) echo '<img src="Red.php" alt="">'.'<br>';
                            }
                        }  

                    foreach (
    $host as $row1 )
                    {                            
                        
    $service->execute([$row1['display_name']]);
                        
                        echo 
    $row1['display_name']."<br>";

                        foreach (
    $service as $row)
                        { 
                            
    //checkboxes                        
                            
    echo '<input type="checkbox" name="list[]" value='.$row['service_object_id'].'>';
                            echo 
    $row['display_name']."<br>";   
                        }  
                        if(isset(
    $_POST["submit"])){
                            
    $_SESSION["submit"] = $_POST["submit"];}
                        if(isset(
    $_POST["list"])){
                            
    $_SESSION['list'] = $_POST['list'];}
                        if(isset(
    $_POST["radio"])){
                            
    $_SESSION["radio"] = $_POST["radio"];}
                        if(isset(
    $_POST["something"])){
                            
    $_SESSION['something'] = $_POST['something'];}
    ?>
    I copied/pasted the display part on the second page, using SESSION instead.

    The code is working fine. I just need to add something more :
    I want that everytime the user fill the input text and click submit button again, the display's result will not replace an old one. I want the results to stay display all the time. If the user fill the input text with an input already entered, it will just compare the new checkboxes' selection.
    I try to look for a solution but can't find one. If you could, please, tell me what should I use or give me an example.

    Another question : The config's page and the display's page will not be on the same computer. I heard that SESSION only work on the same computer, and I should use db's storage instead. What do you think ?

    Thank you for your help.


    PS : If I put "fetchAll();" after "execute();", it doesn't display my query for some reason (even with your code).

  13. #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    PS : If I put "fetchAll();" after "execute();", it doesn't display my query for some reason (even with your code).
    That's because the fetchAll() calls are not storing the fetched data into a php variable (and the code I posted was untested since I don't have any of the database tables or data), but by calling getchAll() (after the execution of the query), all the rows in the result set have been fetched.


    The config's page
    If the purpose of this is to enter and submit configuration information, you have missed the point of the exercise. When you submit the data, for it to be available to anyone else or at any other time, it must be stored on the server, usually in a database table(s).

    I want the results to stay display all the time.
    What results are you referring to?
    Last edited by CFMaBiSmAd; Mar 2nd, 2017 at 03:43 PM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  14. #13
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The result is the image that gonna be displayed after the condition. But I think if I store the result in a db table instead of a session, it will be easier to do.

    I will be back to you tomorrow/monday when I'm finish with the db or if I have problem with it. Thank you

  15. #14
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,378
    Thanks
    10
    Thanked 1,190 Times in 1,180 Posts
    On a side-topic to this discussion ...

    I see lines like this:
    <img src="Green.php" alt="">

    So I assume you are using PHP GD library for generating images on the fly?
    Just curious.

  16. #15
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, my image is just a green circle.

    PHP Code:
    <?php 
    header 
    ("Content-type: image/png");
    $x 11
    $y 11

    $image imagecreatetruecolor($x,$y);

    $green imagecolorallocate($image02550);

    imagefilledellipse($image,5,5,10,10,$green);
    imagepng($image); 
    imagedestroy($image); 
    ?>


 
Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •