将 div 中的页面与 html 表单和 php 链接

发布于 2024-10-20 10:33:28 字数 809 浏览 2 评论 0原文

所以我有这个html代码

<div id="wrap">
<div id="header">

</div>
<div id="content">
    <form method="POST" action="code.php"> 
        Name:
        <input type="text" name="name" size="50">
        <input type=submit value="Get Code">
        </form> 
</div>
<div id="footer">

</div>

用户单击提交到#content div后是否可以加载code.php?

本质上,我想要的是当用户单击提交按钮时,处理后的 code.php 加载到相同的 #content div 上。

因此,在我的 code.php 中,在处理输入的数据后,我想出了这样的代码,

<?php
some processing code here;
$name = 'john';
echo $name;
?>

所以在点击提交后,用户会看到

<div id="content">
john
</div>

希望我没有通过重复自己使我的问题复杂化,请告诉我如果这可以通过 javascript、php 或其他方式实现。

感谢您的阅读!

So I have this html code

<div id="wrap">
<div id="header">

</div>
<div id="content">
    <form method="POST" action="code.php"> 
        Name:
        <input type="text" name="name" size="50">
        <input type=submit value="Get Code">
        </form> 
</div>
<div id="footer">

</div>

Is it possible to load the code.php after the user clicks submit into the #content div?

Essentially, what I want is when the user clicks the submit button, the code.php after processing is loaded onto the same #content div.

So let say in my code.php, after processing the inputted data, I come up with this lilne of code,

<?php
some processing code here;
$name = 'john';
echo $name;
?>

So then after hitting submit, user would see

<div id="content">
john
</div>

Hope I didn't complicate my question by repeating myself, please let me know if this is possible with javascript, php or whatever.

Thanks for the read!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-10-27 10:33:28

@JohnP 是的,$.load 是一个很好的解决方案。但是,您需要在请求中发送表单数据:

更新 [3],用于发送包含多个字段和复选框的 POST:

$('form').submit(function(){

        // create an object to send as a post
        var form = this,
            fields = form.elements,
            el,
            post = {};


        for (var i = fields.length; i--; ) {
            el = fields[i];
            if (el.name) {
                switch (el.type) {
                    case 'checkbox':
                    case 'radio':
                        post[el.name] = (el.checked) ? el.value : '';
                    break;
                    default:
                        post[el.name] = el.value;
                }
            }
        }

    // send the form data in the load request...
    $('#content').load(this.action, post);
    return false;
});

这会将数据作为 POST 发送。

@JohnP yes, $.load is a good solution. However, you'll need to send the form data in the request:

UPDATED [3] for sending a POST with multiple fields and checkboxes:

$('form').submit(function(){

        // create an object to send as a post
        var form = this,
            fields = form.elements,
            el,
            post = {};


        for (var i = fields.length; i--; ) {
            el = fields[i];
            if (el.name) {
                switch (el.type) {
                    case 'checkbox':
                    case 'radio':
                        post[el.name] = (el.checked) ? el.value : '';
                    break;
                    default:
                        post[el.name] = el.value;
                }
            }
        }

    // send the form data in the load request...
    $('#content').load(this.action, post);
    return false;
});

This will send the data as a POST.

七堇年 2024-10-27 10:33:28

既然您已经标记了 jQuery,我将使用 jQuery 示例

$(document).ready(function(){
    $('form').submit(function(){
       $('#content').load('code.php');
       return false;
    })
})

这在这里做了一些假设

  1. 这假设 code.php 与您现在所在的路径相同。
  2. 页面中只有一个表单。
  3. 正如 @johnhunter 指出的,这个例子显然不适用于 post。您可以将帖子数据与方法一起发送。请参阅此处了解用法:http://api.jquery.com/load

编辑

这是一个小提琴示例: http://jsfiddle.net/jomanlk/J4Txg/

它替换包含 jsfiddle/net/echo/html 内容的表单区域(这是一个空字符串)。

注意 2 确保将代码包含在 $(document).ready() 中或将其包含在页面底部。不用说,您的页面中需要 jQuery 才能运行它。

Since you've tagged jQuery, I'll use a jQuery example

$(document).ready(function(){
    $('form').submit(function(){
       $('#content').load('code.php');
       return false;
    })
})

This makes a couple of assumptions here

  1. This assumes that code.php is in the same path that you are in now.
  2. There is only one form in the page.
  3. As @johnhunter points out, this example obviously won't work with post. You can send the post data along with the method. See here for usage : http://api.jquery.com/load

EDIT

Here's a fiddle example : http://jsfiddle.net/jomanlk/J4Txg/

It replaces the form area with the content from jsfiddle/net/echo/html (which is an empty string).

NOTE 2 Make sure to include the code in $(document).ready() or include it at the bottom of the page. It goes without saying you need jQuery in your page to run this.

流年里的时光 2024-10-27 10:33:28

您可能想查看 jquery 表单插件 http://jquery.malsup.com/form/#

You might want to check out jquery form plugin http://jquery.malsup.com/form/#

ゝ杯具 2024-10-27 10:33:28

以简单的方式使用

<div id="content">
   if(isset($_POST['submit'] && !empty($_POST) )
   {
      // do your all post process
      $name ='John';
      echo  $name;
   }
   else {
    <form method="POST" action="$_SERVER['PHP_SELF']"> 
        <label for="uname" >Name:</label><input type="text" name="uname" id="uname" size="50">
        <input type=submit value="Get Code" name="submit">
     </form> 
   }
</div>

in simple way use

<div id="content">
   if(isset($_POST['submit'] && !empty($_POST) )
   {
      // do your all post process
      $name ='John';
      echo  $name;
   }
   else {
    <form method="POST" action="$_SERVER['PHP_SELF']"> 
        <label for="uname" >Name:</label><input type="text" name="uname" id="uname" size="50">
        <input type=submit value="Get Code" name="submit">
     </form> 
   }
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文