简体   繁体   English

传递HTML表单值/加载.php文件

[英]Passing HTML form values / loading .php file

I'm trying to pass the parameter of a form $_POST['txtBody'] into a div via jquery/javascript. 我正在尝试通过jquery / javascript将$ _POST ['txtBody']形式的参数传递到div中。

the PHP file to be loaded: 要加载的PHP文件:

ajax-rewriter.php Ajax的rewriter.php

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    print_r($_POST);
    $articleBody = strtolower($_POST['txt']);
    echo "<pre><b>Original:</b><br /><br /><p>" . $articleBody . "</p></pre>";   
    $word       = "";
    $length     = strlen($articleBody);
    $OutputBody = "";
    for ($i = 0; $i < $length; $i++) {
        $word = $word . $articleBody[$i];
        if ($i == $length - 1)
            $comeCha = " ";
        else
            $comeCha = $articleBody[$i + 1];
        $retStr = getWordPattern($word, $comeCha, "syn/en.syn");
        if ($retStr != "") {
            $OutputBody .= $retStr;
            $word = "";
        }
    }
    echo "<br>";
    echo "<pre><b>Spun:</b><br /><br /><p>" . $OutputBody . $word . "</p></pre>";
}
?>

The HTML form: HTML形式:

    <div id="mainContent"></div>
        <div class="panel panel-primary">
            <div class="panel-heading">Your article rewriter API is: (<span class="results"><b>http://www.wraithseo.com/api.php?rewriter=1&key=<?php echo $user['api_key']; ?></b></span>)</div>
            <div class="panel-body">

                    <form id="frmAjax" action="rewriter.php" method="post" class="form-horizontal container-fluid" role="form">
                    <div class="row form-group">
                        <div class="col-sm-4 text-right"><label for="txtBody" class="control-label">Article:</label></div>
                        <div class="col-sm-8"><textarea class="form-control" id="txtBody" name="txtBody" required="required"></textarea></div>
                    </div>

                    <div class="row form-group">
                        <div class="col-sm-12 text-right">
                            <button type="submit" name="spinText" class="btn btn-default">Spin!</button>
                        </div>
                    </div>                      
                    </form>
            </div>
            <div class="panel-footer">Paste in an article above and hit <b>Spin</b>!</div>
        </div>
<script>
    $(document).ready(function(){
        $('#frmAjax').submit(function(e) {
          e.preventDefault(); // important so the submit doesn't clear the data...
          $.ajax({
                type: "POST",
                url: "ajax-rewriter.php",
                data:{ txt: <?php echo $_POST['txtBody']; ?> }
          })          
          $("#mainContent").load('ajax-rewriter.php')
        });
    });
</script>

I have tried but cannot remember the proper way to pass the $_POST['txtBody'] value to the loaded .php file and show it in the div 我已经尝试过,但是不记得将$ _POST ['txtBody']值传递到已加载的.php文件并将其显示在div中的正确方法

Any help would be appreciated. 任何帮助,将不胜感激。

So it looks like you want to send whatever is typed into the <textarea class="form-control" id="txtBody" name="txtBody" required="required"></textarea> element to be passed to the php file ajax-rewriter.php for processing via JQuery ajax, correct? 因此,您似乎想要发送键入<textarea class="form-control" id="txtBody" name="txtBody" required="required"></textarea>元素的任何内容,以将其传递给php文件ajax-rewriter.php用于通过JQuery ajax处理,对吗?

I don't think you should be trying to get from your $_POST variable inside your AJAX request. 我认为您不应该尝试从AJAX请求中的$ _POST变量获取数据。 Rather, you're sending the value of the textarea through your AJAX request (which is utilizing the POST method to send the data). 相反,您是通过AJAX请求(使用POST方法发送数据)发送textarea的值。

Here is what I believe you are looking for: 这是我相信您正在寻找的东西:

   $(document).ready(function(){
        $('#frmAjax').submit(function(e) {
          var text = $('#txtBody').val();   // ADDED THIS
          e.preventDefault();
          $.ajax({
                type: "POST",
                url: "ajax-rewriter.php",
                data:{textData : text}      // MODIFIED THIS
          })          
          $("#mainContent").load('ajax-rewriter.php')
        });
    });

Then, in your php file, you can get the text variable through the $_POST variable: 然后,在您的php文件中,您可以通过$ _POST变量获取text变量:

$data = $_POST["textData"];

You can then choose what to do whatever you want with the data in your php file. 然后,您可以选择对php文件中的数据执行任何操作。 If you are trying to send the data via ajax to php, modify it via php, then send it back as a response to the ajax request, look into JQuery's success and complete callback functions that are part of the JQuery ajax call. 如果您试图通过ajax将数据发送到php,请通过php对其进行修改,然后将其作为对ajax请求的响应发送回去,请查看JQuery的successcomplete回调函数,这些函数是JQuery ajax调用的一部分。

Here is an example using the success callback: 这是使用成功回调的示例:

   $.ajax({
        type: "POST",
        url: "email.php",
        data: {fullName, phoneNumber, email, comments, response},
        success: function(dataRetrieved){
            $("#response").html(dataRetrieved);
        }
    });

The php file email.php receives the data via $_POST through the ajax request, processes it, and then the email.php file will echo data as output from the server. php文件email.php通过ajax请求通过$ _POST接收数据,对其进行处理,然后email.php文件将echo数据作为服务器的输出。 The data that is echoed is sent back as a response to the ajax request, which can be retrieved as a parameter in the success callback. 回显的数据作为对ajax请求的响应发送回去,可以作为success回调中的参数进行检索。

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

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