HTML Form for PHP, how to return a value instead of displaying a file?

I have created an HTML file containing only a form with a textbox and a submit button.

The form calls php, which checks if the text from the text box represents a case in the database.

Html file:

<form action="index.php" method="post">
<input type="text" name="nome"/>
<input type="submit" value="Rodar"/>
</form>

      

PHP file:

<?php

$nome = $_POST['nome'];
$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw');

if ($pdo->query("SELECT * from renatinho where nome='".$nome."'")->fetchColumn() > 0){
    foreach($pdo->query("SELECT * from renatinho where nome='".$nome."'") as $row) {
        echo $row['id'] . $row['nome'];
    }
    $pdo = null;
}
else{
    echo "Não há ninguém com esse nome.";
}
?>

      

The thing is, when I click the button, it calls and displays the php file. The php file has no layout. I want the results to be displayed in an HTML file, so I don't want the entire new page to display values.

I want the results to be on the same page as the form.

I've tried it for 2 days, but I don't know what exactly to look for. All results obtained are tutorials showing the php file and what I am doing now, but this is not what I want.

+3


source to share


3 answers


You have several options:

  • Combine the two files into one PHP file that has both HTML and PHP. Place your PHP code at the beginning of your script. Check to see if the form is submitted as follows: if ($_POST['submit'] == 'Rodar') { /* process form */ }

    . If the form has not been submitted, your code processing the form will be skipped and the form will only be displayed. If the form was submitted, it will be processed and the form will be displayed again. This is probably the most common solution.
  • After processing the form in your PHP file, redirect back to the HTML file with header('Location: html_file.html'); die();

    . Just make sure you don't output anything to the screen before doing this (for example echo

    ).
  • You can submit your form using AJAX. This is a JavaScript call over HTTP to your server without reloading the page. Just Google AJAX and you will find a boatload of tutorials.


For clarification, it seems like you can't figure out that you can combine HTML and PHP in a .php file. You can do it. You just either echo

HTML to the screen or exit the tag <?php

with a tag ?>

and start writing your HTML.

0


source


You should be using AJAX to send a request to the server, perhaps to display data on the page.

@worldofjr makes a good point; you can check the data $_POST

to see if something was submitted if you are ok with the user who has accessed the page again



<?php
if(isset($_POST['fieldname'])){
  //whatever code here
}

?>
<form method="post">
  ...inputs here...
</form>

      

0


source


You can only use PHP / HTML to solve this problem. You can add the following to your form page (which should be a PHP page, but it's easy to do).

To check if the user has submitted the form, you should see if there is data in one of the POST variables using if(isset($_POST['nome'])

(where nome

is one of the fields).

if(isset($_POST['nome'])) {
  $nome = $_POST['nome'];
  $pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw');

  if ($pdo->query("SELECT * from renatinho where nome='".$nome."'")->fetchColumn() > 0){
    foreach($pdo->query("SELECT * from renatinho where nome='".$nome."'") as $row) {
        echo $row['id'] . $row['nome'];
    }
    $pdo = null;
  else {
    echo "Não há ninguém com esse nome.";
  }
}

      

As for your form, to submit the form to the same page, just don't set the attribute action

(in HTML5).

<form method="post">

      

(Don't forget to put <!doctype html>

at the top of the page to use HTML5)

Hope it helps.

0


source







All Articles