I need a button to run this test print label php script after the form is posted with the user selection. What am I missing? There is no button at the end of the displayed list.
Do I need to run the same mysql query again to get the values to send to the print script? What am I missing? Do I need a form? I just need a simple javascript button to click that will change the location and run the php page but I can't figure out how to get the onclick button? Do I need an image reference?tnx,

if ( !$_POST ) {
getIt();
}else{
showIt();

    echo "<script type='text/javascript'>";
    echo "<span onclick=\"location.href='print_test.php';\">link</span>";
    echo "</script>";

}

I also need something in the button to indicate the count of records displayed whether the number of records were over 500. If they are then I have to print the next set of records on the next page because the printer can only handle 500 at a time.

    I am going to rethink this and try to get it to print on the same page.

      You weren't too far off, although you were setup to have text that you could click, not a button.

      the first major thing, and why your span probably isn't showing up, is your html is inside of script tags. The onclick event doesn't have to be inside script tags.

      <span onclick="alert('you clicked me!');">Click Here</span>
      
      //as opposed to how you had it
      <script>
      <span onclick="alert('you clicked me!');">Click Here</span>
      </script>
      
      

      Secondly, if you want a button, you will have to add the onclick to the appropriate element for a button, which is an input.

      <input type="button" value="text on the button" name="name_in_post_or_get"  onclick="alert('you clicked the button!);"/>
      

      You would do some logic in PHP to build the text you want displayed on the button, then set that text as the buttons value.

      Based on some other things you said I don't think this will get you all the way there, as I think you are going to need some extra logic and a form to post the previous record you stopped on, so you can grab the next 500 at the offset. At the very least you can now have a button with a working onclick 🙂

        I really appreciate the answer even though I closed the thread because now I still need the button. I did rework the FTP script so it works better but I still need the button thanks. I just didn't want to ask a question that I wasn't sure about but it turns out I really need that little button to work.

          I now have the button on the right spot, but can't get the script to fire.
          I don't know what GET or POST to put in the name= xxxx??

          I have a show_it or get_it if/else at the top of the page. I tried putting $me in the name=$me
          which is SERVER['php_self'] posted to the same page. It still doesn't fire the print_labels.php page. If the search criteria is posted then the show_it function gets called. Is this what you mean about the GET or the POST?

          $me=$_SERVER['PHP_SELF'];
          echo "<form method='post' enctype='multipart/form-data' action='$me' onsubmit='return validate_form(this);'>\n";

          if ( !$_POST ) {
          getIt();
          }else{
          showIt();

          }

          echo "</form>\n";
          echo "</body>\n";
          echo "</html>\n";
          The button is in the show_it function. So if the search criteria is posted to the page this page then the sql query happens and the results are displayed.
          It writes the records to printer template with printer language and the variables to print on the labels. My print script just sends that text file to the printer after it gets closed. The create template function gets called for each record
          and a line is written to the text file. All I need to do is fire that php page once the while loop ends and the script works. I just have to hit that page when the user clicks it. I should be able to write a javascript that only allows 500 on a page then the text file will be the right size. I just need to hit the page and can't figure out why it isn't?
          that

          echo "<table border='1'>\n";
          $path = "zebra/"; //setting search path
          $fp=fopen($path."template.prn","w");
          while ($row = mysql_fetch_array($result)) {

           $data=$row['$data'];
          
          ....................
          
              echo "</tr>\n";

          }//while end
          fclose($fp);
          echo "</table>\n";
          echo "<input type='button' value='Print Labels' align='center' name=$me onclick='print_test.php'/>";
          }//end function show it

          Am I supposed to make a form around this javascript ? I could make it a get with the value of yes for print would that work?
          if onclick = yes then ?=yes,print_test.php something like that. Thanks,

            It's not working now because your onclick isn't formatted correctly. You are giving it just a php file name.

            echo "<input type='button' value='Print Labels' align='center' name=$me onclick='print_test.php'/>";
            

            Here is how to fix that, but I think you may want to do something a little bit differently.

            echo "<input type='button' value='Print Labels' align='center' name=$me onclick='document.location=\'print_test.php\''/>";
            

            Now when the button is clicked, that will go to print_test.php correctly. The reason I don't think that is going to work, is you don't end up with any post/get vars, because a button does not submit a form. So if you were going to print records based on an id that was a part of the form, it would make it to print_test.php because a form is not posting them.

            I think it may be easier for you to build the form inside your if statement. Then you can decide based on showIt/getIt what file location the form will post to. Then use a submit button instead of just a button, and your form will post to print_test.php. That means all the post/get vars will make it, and you can grab the records based on the id.

            Something like this for the first part

            
            
            if ( !$_POST ) {
            //empty action is the same as posting back to the same page.
            echo "<form method='post' enctype='multipart/form-data' action='' onsubmit='return validate_form(this);'>\n";
            getIt();
            }else{
            //So here we will post the form with the action as where we want it to go
            //when we press print.  You will need to set the form up inside the showIt to
            //get the values though.
            echo "<form method='post' enctype='multipart/form-data' action='test_print.php' onsubmit='return validate_form(this);'>\n";
            showIt();
            
            }
            

            Make sense? How you have it currently the data you are trying to print won't make it to the print.php page. So you have to somehow get the request to the print page, this is probably goign to mean in the showIt function converting what used to be the user input forms to be hidden inputs with the values of the user form.

              I can't get it to work.
              Is this right?
              echo "<input type='button' value='Print Labels' name=$me onclick='document.location=\'print_test.php\''/>";
              It does not hit the page or change location?
              Maybe the post isn't $me or maybe there is a quotation that is wrong?
              I think it should work but I do see another problem.
              I don't need any vars. The reason is I write them to a file.
              This script just FTP's the text file with the vars in it to a printer.
              So all I need to do is hit the page. The script uploads the local file
              and sends it on to the printer :-)
              The problem I can see is that the user could hit the print button more than once and generate double or triple labels just by clicking the button because the text file will still be there. I guess that would be my answer for that when the print button gets clicked I can write over the text file so they get only one chance. I probably should have a cancel button but I can think about that.
              I need a way to make sure they can only print it once or they have to do another search. The printer keeps the text file for a little while then deletes it. What would be awesome would be a button that suppresses after it is clicked and you can't click it again.

                Escaping might be messed up, so yeah something to do with quotes.

                I usually do this, this test one works. Brain isn't cooperating today, sorry to have messed you up ><

                echo "<input type=\"button\" name=\"test\" value=\"test\" onclick=\"alert('hi');\"/>";
                

                  Sorry, It still doesn't click the button and print?
                  I added the . to escape the .'s because with double quotes around something with a "." dot in it that becomes the concatenation character in php.

                  This looks right to me but it still doesn't work. Maybe you can read it better with the php tags.

                  echo "<input type=\"button\" name=\"Print\" value=\"print\" onclick=\"document\.location=\"print_test\.php\";\"/>";
                  

                  Can you check this one more time it still doesn't print. I think I almost have it?
                  thanks,

                    We're escaping the quotes so PHP won't see the '.', so you don't want to escape it. Escaping as you've discovered breaks the javascript.

                    You are correct if we did not escape the double quotes however.

                      Res;10955755 wrote:

                      We're escaping the quotes so PHP won't see the '.', so you don't want to escape it. Escaping as you've discovered breaks the javascript.

                      You are correct if we did not escape the double quotes however.

                      So you need the double quotes in javascript....
                      Here is my latest attempt which still doesn't work.
                      I left it all double quotes and left the dots "."'s alone
                      echo "<input type=\"button\" name=\"Print\" value=\"print\" onclick=\"document.location=\"print_test.php\";\"/>";

                      the php script does print when you type the page in the browser.
                      I'm guessing the problem is somewhere around the double quotes around print_test.php inside the double quotes around the document.location
                      thanks,

                        Closer 🙂

                        We now have the PHP in double quotes, inside the javascript in the onclick, we want to use single quotes.

                        echo "<input type=\"button\" name=\"Print\" value=\"print\" onclick=\"document.location='print_test.php';\"/>";
                        

                        See how the onclick is surrounded in double quotes? So when it comes to surrounding the string in javascript, we do it with single quotes. Since those are inside of double quotes (the start and end of our echo statement) they do not need to be escaped.

                          thanks so much for your patience and the little javascript lesson, i'm going to need it this weekend. I got it. I was thinking in php not javascript. I even got an hourglass while the script ran and it printed the labels. this is big!

                            Good shtuff. Hope the weekend goes well for you 🙂

                            Reply to this thread Monday if you get stuck again, I get the emails and check up.

                              Write a Reply...