Two or more PHP formats on the same page

Asked

Viewed 5,822 times

3

I’m having a little problem, I’m creating an app and I intend to have two Forms on the same page. It turns out that when I run the first one, I have no problems, but when I run the second one, the first one is done again, and since there is no data there is an error. To try to get around this problem I created an Hidden input:

<input type="hidden" name="opti" value="1">
<input type="hidden" name="opti" value="2">
<input type="hidden" name="opti" value="3">

The first line being on the first form and the other two are on the second. I intended that although they were on the same page they did not conflict and for this I created a cycle if:

if (isset($_POST['opti']) == '1') 
    {

        // Executa o calculo quando existir


            if ($_SERVER['REQUEST_METHOD']== "POST")
            {
                $fabric = $_POST['Fabric'];
                $spare = $_POST['Spare'];
                $xres = $_POST['Xres'];
                $yres = $_POST['Yres'];
                $mspeed = $_POST['Mspeed'];
                $lents = $_POST['Lents'];
                $sensor = 1280;

                Calculos
            }
     }elseif (isset($_POST['opti']) == '2') 
    { tabela}

So when I press the first form, I can perform all calculations without problem, but then when I want to create the table, with the second form, returns me to execute the first and as the fields are blank gives error.

Thank you.

Page code is here: http://jsfiddle.net/akjs061o/

  • 2

    Put the rest of your code here, the full page.

  • 2

    add the code of <form>, only with the <input> it’s hard to know the problem

  • To run your code in PHP, you can publish on this site: http://www.viper-7.com/rCOpJR

  • That one Calculos is breaking your code.

  • This word Calculus is only to indicate that calculations are made in this space, because it is the only thing that I cannot show. I ask you to understand. Thank you.

  • 1

    There is a conceptual problem in the question. They are not PHP Forms, they are HTML Forms like any other. PHP only processes their output (this would help understand the problem). Starting from this, there is another thing: the problem may be in HTML, which was not posted in the question. It would be legal [Dit] to ask the question and put all the relevant part. It is an apparently simple problem to solve, but the lack of information makes it difficult.

  • Hello Bacco, first of all thank you for your interest. My entire website is here: http://jsfiddle.net/akjs061o/ as stated above, only the calculations are not present. If you know how to solve the problem would appreciate a tip. Thank you

Show 2 more comments

1 answer

2

UPDATE

I updated the code. Now the values are passed to a PHP page I called calculo.php. You just need to create this page and put your calculations on it, then take the result of this page and put it where you want on the current page.

JSFIDDLE

This is just one example!

ORIGINAL

Well, that’s your original code edited for the Submit on the same page.

Some Observations:

  1. Do not use isset($_POST) to check if a form has been submitted. It is best to use $_SERVER['REQUEST_METHOD'];
  2. In PHP it is not necessary to declare variables, as it is a dynamic language (Dynamic type), this is a bit boring, but so you can develop more quickly, since PHP was created for RAD (Rapid Application Development).
  3. To Caesar what is Caesar: let Javascript and CSS do the whole client side, without fear. The server should only worry about the requests that are intrinsic to it, such as file and database requests. While the client machine is making a request on the server, your server may be receiving hundreds. This is Web 2.0!
  4. Whenever possible include Javascript codes at the end of the file, but always between tags <body></body>. Thus, it will be possible for the DOM (Document Object Model) be loaded and there yes the Javascript codes.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Calculations</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <style>
            table thead tr th, table tbody tr td{
                text-align: center;
            }
            .container{
                width: 75%
            }
        </style>
        <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
        <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <nav class="navbar navbar-static-top">

        </nav>
        <div class="container">
            <div class="line">
                <header class="header no-print" style="border-bottom: 2px solid gray; margin-bottom: 35px;">
                    <div class="row">
                        <form class="form-horizontal" name="formulario" method="POST">
                            <div class="container" id="decrease-input">
                                <div class="col-sm-6">
                                    <div class="form-group col-lg-12">
                                        <label for="Fabric">Fabric Wide</label>
                                        <input id="Fabric" name="Fabric" type="text" placeholder="mm" class="form-control input-md">
                                    </div>
                                    <div class="form-group col-lg-12">
                                        <label for="Spare">Spare width</label>  
                                        <input id="Spare" name="Spare" type="text" placeholder="mm" class="form-control input-md">
                                    </div>
                                    <div class="form-group col-lg-12">
                                        <label for="Xres">Resolution X</label>  
                                        <input id="Xres" name="Xres" type="text" placeholder="mm/pixel" class="form-control input-md">
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group col-lg-12">
                                        <label for="Yres">Resolution Y</label>  
                                        <input id="Yres" name="Yres" type="text" placeholder="mm/pixel" class="form-control input-md">
                                    </div>
                                    <div class="form-group col-lg-12">
                                        <label for="mSpeed">Fabric Max speed</label>  
                                        <input id="mSpeed" name="Mspeed" type="text" placeholder="m/min" class="form-control input-md">
                                    </div>
                                    <div class="form-group col-lg-12">
                                        <label for="Fabric">Lenses</label>  
                                        <input id="lent" name="Lents" type="text" placeholder="mm" class="form-control input-md">
                                    </div>
                                </div>
                                <div class="col-sm-12" style="text-align: center">
                                    <div class="form-group">
                                        <button type="reset" id="Bt5" name="Bt5" class="btn btn-inverse">Reset</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="resposta"></div>
                    </div>
                </header >
                <div class="container frame print html2pdf">
                    <header class="header no-print" style="margin-bottom: 40px;">
                        <img class="right2" src="http://placehold.it/100x65" onclick="javascript:window.print();">
                    </header>
                    <header class="header print">
                        <!-- Text input-->
                        <div class="row">
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="Company">Company</label>  
                                <div class="col-md-4">
                                    <input id="Company" name="Company" type="text" placeholder="Name" class="form-control input-md size">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <!-- Text input-->
                            <div class="form-group">
                                <label class="col-md-3 control-label size" for="Date">Date</label>  
                                <div class="col-md-4">
                                    <input id="Date" name="Date" type="text" placeholder="DD / MM / AAAA" class="form-control input-md size">
                                </div>
                            </div>
                        </div>
                        <table class="table table-bordered">
                            <caption> Input </caption>
                            <thead>
                                <tr>
                                    <th>Fabric Wide</th>
                                    <th>Spare width</th>
                                    <th>Resolution X</th>
                                    <th>Resolution Y</th>
                                    <th>Max speed</th>
                                    <th>Lenses</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <!-- as celulas abaixo recebem os valores dos formularios acima -->
                                    <td><?php echo $fabric . "mm"; ?></td>
                                    <td><?php echo $spare . "mm"; ?></td>
                                    <td><?php echo $xres . "mm/px"; ?></td>
                                    <td><?php echo $yres . "mm/px"; ?></td>
                                    <td><?php echo $mspeed . "m/min"; ?></td>
                                    <td><?php echo $lents . "mm"; ?></td>
                                </tr>
                            </tbody>
                        </table>
                        <table class="table table-bordered">
                            <caption> Calculation </caption>
                            <thead>
                                <tr>
                                    <th>Cam number</th>
                                    <th>Space between cam</th>
                                    <th>FOV</th>
                                    <th>Overlap</th>
                                    <th>Height</th>
                                    <th>Pixels</th>
                                    <th>Selected</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><?php echo $ncam; ?></td>
                                    <td><?php echo $dist . "mm"; ?></td>
                                    <td><?php echo $fov . "mm"; ?></td>
                                    <td><?php echo $ov . "mm"; ?></td>
                                    <td><?php echo $alt . "mm"; ?></td>
                                    <td><?php echo $sensor; ?></td>
                                    <td><input type="radio" name="radio" class="rdo" value="1" id="l1" data-col="1"></td>
                                </tr>
                                <tr>
                                    <td><?php echo $ncam1; ?></td>
                                    <td><?php echo $dist1 . "mm"; ?></td>
                                    <td><?php echo $fov1 . "mm"; ?></td>
                                    <td><?php echo $ov1 . "mm"; ?></td>
                                    <td><?php echo $alt1 . "mm"; ?></td>
                                    <td><?php echo $sensor; ?></td>
                                    <td><input type="radio" name="radio" class="rdo" value="2" id="l2" ></td>
                                </tr>
                            </tbody>
                        </table>
                        <!-- abaixo temos a tabela resultante dos calculos -->
                            <table border="0" class="table table-bordered text-center">
                                <thead>
                                    <tr>
                                        <th>Camera</th>
                                        <th>Left</th>
                                        <th>Right</th>
                                    </tr>
                                </thead>
                                <tbody>
                            </tbody>
                        </table>
                    </header>
                </div>
                <button class="btn btn-primary" type="submit" name="gerar_relatorio" id="gerar_relatorio">Relatorio</button>
            </div>
            <hr>
            <footer>
                <p>&copy; Company - Elbit Vision Systems ltd (2015)</p>
            </footer>
        </div>

        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
        <script src="http://mrrio.github.io/jsPDF/dist/jspdf.debug.js" type="text/javascript"></script>
        <!-- depois se quiser gerar PDF a partir do canvas, usa isso aqui-->
        <script type="text/javascript" src="//cdn.rawgit.com/niklasvh/html2canvas/0.5.0-alpha2/dist/html2canvas.min.js"></script>
        <script type="text/javascript">
            $(function(){
                $('form[name="formulario"] input').keydown(function(){
                    //capturando os valores dos formularios
                    var dados_do_formulario = $('form[name="formulario"]').serialize();
                    //enviando os valores por meio do metodo POST para a pagina de calculo[.php]    
                    $.post("calculo.php", {dados: dados_do_formulario}, function(resposta){
                        $(".resposta").html(resposta);
                    });
                });
                
                //gerar PDF
                $('#gerar_relatorio').click(function(){
                    var pdf = new jsPDF('p', 'pt', 'letter'),
                        id_da_parte_html = $('.html2pdf')[0]; //pode ser qualuer seletor na verdade
                    
                    
                    margem = {
                        top: 80,
                        bottom: 60,
                        left: 40,
                        width: 522
                    };
                    pdf.fromHTML(
                    id_da_parte_html,
                    margem.left,
                    margem.top, {
                        'width': margem.width
                    },

                    function (resultado) {
                        pdf.save('teste_html2pdf.pdf');
                    }, margem);

                });
            });
        </script>
    </body>
</html>

Tips:

Access the good practice PHP code site ;)

Take this example with Javascript generating dynamic forms

  • First of all thank you for the reply. I have been testing and unfortunately the result is the same. The intention is that where company says and date is only a text field for when printing get the reference. Only the table with the radial input is that belong to the form. If I send the first one everything is calculated right, but when I send the second page it’s like a Reload and I get zero values when I want to continue the calculations by selecting either one or the other option

  • Inside the{}while() loop has a problem. The word "Calculus", next to while() has not been escaped or commented.

  • Thank you for helping me improve my application. But I still have the same problem. when I press the Ubmit of the second form, I return to the values all to zero, not keeping the previously calculated data to proceed with the following calculations.

  • I’m working on it. I’ll take advantage that you’re using Bootstrap, it helps a lot! By the way, you can tell me if the systemsibar.php page is the same as the current one or is it another page?

  • First of all thank you. Yes it’s all the same page, maybe that’s why I’m having problems. Thank you so much for the help.

  • I edited your code. You can test it works, but the form data is not saved as it is submitted through POST. I highly recommend that you use AJAX or Javascript only to generate the calculations in the View pro user, only when it is necessary Submit is that you should use PHP.

  • First of all, when you say you edited the code, you are referring to http://jsfiddle.net/akjs061o/?

  • I didn’t edit Jsfiddle because I’ve never used it before and I don’t know if it runs PHP. Anyway, you can take the code from the snippet above and make the changes to complete the work.

  • When I answered I had not updated the page and had not seen the changes. I understand the idea that you are trying to pass me, create only one form, but in this case I needed it not to be all together, because some calculations are made initially to choose the number of cameras and then selected the desired option and continued with the calculations. Thank you

  • @Pedrolima Can you explain to me what you are trying to do? Do you intend to save the data? What are the rules for the field calculations. Later I put a solution with jQuery. It’s ideal!!! Well, now I have to go! Then I’ll come back.

  • I am creating an application with Phpdesktop, to be able to use in my work and after testing and be something advantageous to share within it. I do not wish to use any database at this time. The idea is that with the first form calculate the distance between cameras, the height to the fabric, the amount of pixels to use and first create a table with the summary of the form and then another with the results of the calculations. In this second table I wanted to put another form, because I could choose 9 or 10 cam and so proceed and create a final table. In the end physically print the pdf.

  • That is why at this time the only thing I wanted was to be able to execute the second form without having to perform a Reload on the whole page, thus allowing to keep all the calculations used and not to be changed to zero. Only when I do the second form Ubmit and that starts the problems. Yes I know I could do in javascript, but my knowledge is very limited in this language so start with php and then when I had something functional and can learn, I would do the same in Javascript and can even improve. Thanks for the help.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.