简体   繁体   English

如何在表单内调用php文件(html)

[英]How to call php file inside a form (html)

I have 2 PHP files, called: 我有2个PHP文件,名为:

  • user_one.php -> returns a JSON with the info of One user (given by POST parameter) user_one.php- >返回带有一个用户信息的JSON(由POST参数赋予)
  • user_uptade.php -> modify all fieds of One user (given by POST parameter) and returns a JSON with the modified data. user_uptade.php- >修改一个用户的所有文件(通过POST参数给定),并返回带有已修改数据的JSON。

Then I got 1 more file called, update_user.html where I want to: 然后,我想在其中再添加一个名为update_user.html的文件:

  1. Call user_one.php 呼叫user_one.php
  2. take the JSON and put the info in the form of the update_user 使用JSON并将信息以update_user的形式放置
  3. When User modifies his data, when he click accept button, user_update.php has to be called. 用户修改数据时,单击接受按钮时,必须调用user_update.php。

What I want to know is how can I fill this form calling the user_one.php given the $_POST['user'] . What I want to know is how can I fill this form calling the user_one.php given the $_POST['user']

*The user_one.php has to return a "echo json" because this file is called ina mobile phone request* * user_one.php必须返回“ echo json”,因为此文件在手机请求中称为*

user_one.php (where the json is coded and returned) user_one.php(在其中json编码并返回)

    <?php

/*
 * Following code will get single product details
 * A product is identified by product id (uid)
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '../../db_connect.php';

// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_POST["user"])) {
    $uid = $_POST['user'];
    // get a product from products table
    $result = mysql_query("SELECT *FROM Usuarios WHERE Usuario = '$uid'");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $users = array();
            $users["name"] = $result["Nombre"];
            $users["lastname"] = $result["Apellidos"];
            $users["user"] = $result["Usuario"];
            $users["password"] = $result["Contrasena"];
            $users["birthday"] = $result["Fnacimiento"];
            $users["direction"] = $result["Direccion"];
            $users["email"] = $result["Email"];
            $users["phone"] = $result["TelfFijo"];
            $users["mobile_phone"] = $result["TelfMov"];
            $users["user_type"] = $result["TipoUsuario"]; 
            // success
            $response["success"] = 1;
            // user node
            $response["user"] = array();
            // push single users into final response array
            array_push($response["user"], $users); 
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no user found
            $response["success"] = 0;
            $response["message"] = "No user found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no user found
        $response["success"] = 0;
        $response["message"] = "No user found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

user_uptade.html (Where has to be filled the form with JSON obtained in user_one.php) user_uptade.html(必须使用在user_one.php中获得的JSON填写表单)

<html>
<body> 

<br>Agregue un nuevo Usuario:

<form method="post" action="user_update.php" >
Nombre:
<br>
<input type="Text" name="name" >
<br>
<br>
Apellidos:
<br>
<input type="Text" name="lastname" >
<br>
<br>
Usuario:
<br>
<input type="Text" name="user" >
<br>
<br>
Fecha nacimiento:
<br>
<input type="Text" name="bday" >
<br>
<br>
Direccion:
<br>
<input type="Text" name="direccion" >
<br>
<br>
TelFijo:
<br>
<input type="Text" name="tfijo" >
<br>
<br>
TelMov:
<br>
<input type="Text" name="tmov" >
<br>
<br>
Email:
<br>
<input type="Text" name="email" >
<br>
<br>
Password:<br>
<input type="password" name="password" ><br>

<input type="Submit" name="enviar" value="Agregar">

</form>

</body>
</html>

You cannot 'call' PHP files from HTML. 您不能从HTML“调用” PHP文件。 You can only do that with PHP files after using include or require to load the other PHP file you want to use. 仅在使用include或要求加载要使用的其他PHP文件之后,才可以对PHP文件执行此操作。 The only thing, that PHP can actually do is post a form to server, which can be done like this: PHP真正可以做的唯一一件事就是将表单发布到服务器,可以这样完成:

<form type="post" action="http://yoururl.com/your_desired_php_file.php">
...
</form>

UPDATE: you can easily update your HTML file to PHP by changing the extension from .html to .php After that you can use require("your_filename.php"); 更新:通过将扩展名从.html更改为.php,您可以轻松地将HTML文件更新为PHP。之后,您可以使用require(“ your_filename.php”); to load the file you want to use. 加载要使用的文件。 And then you can call any function or access any global variables from that file. 然后,您可以调用该文件中的任何函数或访问任何全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM