HTML表单到PHP如何返回值而不是显示文件?

I've made a HTML file containing only a form with a textbox and a submit button.

The form calls a php that will check if the text from the textbox represents a register 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 press the button it calls and displays the php file. The php file has no layout. I want the results to be displayed in the HTML file, cause i don't want to make a whole new page just to display the values.

I want the results to be in the same page where the form is.

I've being googling it for 2 days, but I don't know what exactly to search. All the results I got are tutorials that displays the php file, and that's what I'm doing now but that's not what I want.

You have a few options:

  1. Combine the two files into one PHP file that has both HTML and PHP. Put your PHP code at the top of the script. Check if the form is being submitted like this: if ($_POST['submit'] == 'Rodar') { /* process form */ }. If the form was not submitted, your code that processes the form will be skipped and the form will just be displayed. If the form has been submitted, it will be processed and the form will be displayed again. This is probably the most common solution.
  2. After processing the form in your PHP file, redirect back to the HTML file using header('Location: html_file.html'); die();. Just make sure you don't output anything to the screen before doing this (ie. echo).
  3. You could submit your form using AJAX. This is a JavaScript call over HTTP to your server without reloading the page. Just google AJAX and you'll find a boatload of tutorials.

For clarification, it seems like you may not realize that you can combine HTML and PHP in a .php file. you can do this. You just either echo the HTML to the screen, or break out of your <?php tag with a ?> tag and start writing your HTML.

You should use AJAX to send a request to the server, possibly returning data to show on the page.

@worldofjr makes a good point; you can check $_POST data to see if something was submitted, if you're OK with the user making another request to the page anyway

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

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

You can use just PHP/HTML to solve this. You can add the following to the page with the form on (which must be a PHP page, but that'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 your form to the same page, just don't set the action attribute (in HTML5).

<form method="post">

(Remember to put <!doctype html> at the top of your page so that HTML5 is used)

Hope this helps.