如何将表单发布到php页面?

时间:2022-03-24 01:56:52

I want to post a form with two fields on a php page using $_POST['json']; the object will be json type. Can you help me?

我想使用$ _POST ['json']在php页面上发布一个包含两个字段的表单;该对象将是json类型。你能帮助我吗?

<div id="result"></div>
<form name="form" id="form" method="post" >
    Name: <input type="text" name="name" id="name"/><br/>
    Number: <input type="text" name="number" id="number"/><br/>
    <input type="button" value="Submit!" name="submit" id="submit"/>
</form>

1 个解决方案

#1


2  

If you're trying to send json via post, you need to script it using js or jquery

如果你试图通过post发送json,你需要使用js或jquery来编写脚本

throw JSON.stringify(values) into your function. You can grab the form data fast with var values = $("form").serialize();.

将JSON.stringify(values)抛出到您的函数中。您可以使用var values = $(“form”)快速获取表单数据.serialize();.

I think you're really asking about ajax or php post returning json... This example should cover both scenarios with some tweaking.

我认为你真的在询问ajax或php post返回json ...这个例子应该涵盖两种情况并进行一些调整。

index.php

<?
    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $number = isset($_POST["number"]) ? $_POST["number"] : "";
    $fromAjax = isset($_POST["ajax"]) ? $_POST["ajax"] : NULL;
    if($name != "" || $number != ""){
        $results = array(
            'name' => $name,
            'number' => $number
        );

        $resultFromPHP = json_encode($results);
    }
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div id="resultfromphp" style="border: 1px solid;width:300px;height:25px; ">
    <? echo isset($resultFromPHP) ? $resultFromPHP : ""; //if result is set, echo it, else empty?>
</div>
<br>
<div id="resultfromajax" style="border: 1px solid;width:300px;height:25px;">
</div>
<hr>
<form name="form" id="form" method="post" >
    Name: <input type="text" name="name" id="name"/><br/>
    Number: <input type="text" name="number" id="number"/><br/>
    <input type="submit" value="PHP Submit!" name="php_submit" id="php_submit"/>
    <input type="button" value="AJAX Submit!" name="ajax_submit" id="ajax_submit"/>
</form>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="submit.js"></script>
</body>
</html>

for the php method, you put the php code in the document itself, and submit to self. you can use includes to keep the code separated. Using the html submit, you can't convert the form data before sending. What I wrote at the top should cover that. what will happen here is the data is sent post, and converted to json there.

对于php方法,你将php代码放在文档本身,并提交给自己。您可以使用includes来保持代码分离。使用html提交,您无法在发送之前转换表单数据。我在顶部写的应该涵盖那些。这里会发生什么是数据发送后,并转换为json那里。

ajax method is pretty much the same, however since you're using jquery with this part you can make json first, send and receive json back.

ajax方法几乎是一样的,但是因为你在这个部分使用jquery,你可以先使用json,然后发送和接收json。

submit.js

$().ready(function(){
    $("#ajax_submit").on("click", function(){
        sendSomeAjax();
    });
});

var sendSomeAjax = function(){
    var sendData = $("form").serialize();
    $.ajax({
        url: "submit.php",
        type: "post",
        data: sendData,
        success: function(data){
            $("#resultfromajax").html(data);
        },
        error: function(data){
            $("#resultfromajax").html("There was an error.");
        }
    });
};

submit.php

<?php

    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $number = isset($_POST["number"]) ? $_POST["number"] : "";
    $fromAjax = isset($_POST["ajax"]) ? $_POST["ajax"] : NULL;
    if($name != "" || $number != ""){
        $results = array(
            'name' => $name,
            'number' => $number
        );

        echo json_encode($results);
    }

#1


2  

If you're trying to send json via post, you need to script it using js or jquery

如果你试图通过post发送json,你需要使用js或jquery来编写脚本

throw JSON.stringify(values) into your function. You can grab the form data fast with var values = $("form").serialize();.

将JSON.stringify(values)抛出到您的函数中。您可以使用var values = $(“form”)快速获取表单数据.serialize();.

I think you're really asking about ajax or php post returning json... This example should cover both scenarios with some tweaking.

我认为你真的在询问ajax或php post返回json ...这个例子应该涵盖两种情况并进行一些调整。

index.php

<?
    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $number = isset($_POST["number"]) ? $_POST["number"] : "";
    $fromAjax = isset($_POST["ajax"]) ? $_POST["ajax"] : NULL;
    if($name != "" || $number != ""){
        $results = array(
            'name' => $name,
            'number' => $number
        );

        $resultFromPHP = json_encode($results);
    }
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div id="resultfromphp" style="border: 1px solid;width:300px;height:25px; ">
    <? echo isset($resultFromPHP) ? $resultFromPHP : ""; //if result is set, echo it, else empty?>
</div>
<br>
<div id="resultfromajax" style="border: 1px solid;width:300px;height:25px;">
</div>
<hr>
<form name="form" id="form" method="post" >
    Name: <input type="text" name="name" id="name"/><br/>
    Number: <input type="text" name="number" id="number"/><br/>
    <input type="submit" value="PHP Submit!" name="php_submit" id="php_submit"/>
    <input type="button" value="AJAX Submit!" name="ajax_submit" id="ajax_submit"/>
</form>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="submit.js"></script>
</body>
</html>

for the php method, you put the php code in the document itself, and submit to self. you can use includes to keep the code separated. Using the html submit, you can't convert the form data before sending. What I wrote at the top should cover that. what will happen here is the data is sent post, and converted to json there.

对于php方法,你将php代码放在文档本身,并提交给自己。您可以使用includes来保持代码分离。使用html提交,您无法在发送之前转换表单数据。我在顶部写的应该涵盖那些。这里会发生什么是数据发送后,并转换为json那里。

ajax method is pretty much the same, however since you're using jquery with this part you can make json first, send and receive json back.

ajax方法几乎是一样的,但是因为你在这个部分使用jquery,你可以先使用json,然后发送和接收json。

submit.js

$().ready(function(){
    $("#ajax_submit").on("click", function(){
        sendSomeAjax();
    });
});

var sendSomeAjax = function(){
    var sendData = $("form").serialize();
    $.ajax({
        url: "submit.php",
        type: "post",
        data: sendData,
        success: function(data){
            $("#resultfromajax").html(data);
        },
        error: function(data){
            $("#resultfromajax").html("There was an error.");
        }
    });
};

submit.php

<?php

    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $number = isset($_POST["number"]) ? $_POST["number"] : "";
    $fromAjax = isset($_POST["ajax"]) ? $_POST["ajax"] : NULL;
    if($name != "" || $number != ""){
        $results = array(
            'name' => $name,
            'number' => $number
        );

        echo json_encode($results);
    }