加载/执行.php文件

时间:2017-11-05 18:03:12

标签: javascript php ajax

我已经在这里待了几个小时了,我很确定这是我身边的语法错误,我试图调用.php文件并执行内容,我和#39; m传递 textarea 值,以便在.php代码中使用。

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.example.com/api.php?spinner=1&key=<?php echo $user['api_key']; ?></b></span>)</div>
        <div class="panel-body">

                <form id="frmAjax" action="spinner.php" method="post" class="form-horizontal container-fluid" role="form">

                    <div class="row form-group">
                        <div class="col-sm-4 text-right">
                            <label for="txtLanguage" class="control-label">Language:</label>
                        </div>
                        <div class="col-sm-8">
                            <select id="txtLanguage" name="txtLanguage" class="form-control" required="required">
                                <?php
                                $level = array("syn/en.syn" => "English", "syn/de.syn" => "German", "syn/pl.syn" => "Polish");
                                ?>
                                <?php foreach ($level as $key => $value) { ?>
                                    <option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>                  

                    <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) {
          var text = $('#txtBody').val();  
          $.ajax({
                 url: "ajax-rewriter.php",
                type: "POST",
                data: { textData : text }
                })       
        });
    });  
    $("#mainContent").load('ajax-rewriter.php') 
    e.preventDefault();  
</script>

Ajax的rewriter.php

<?php 

include('includes/db_connection.php');
include('includes/sessions.php');
include('includes/functions.php');

if (isset($_REQUEST['textData')) {

    $articleBody = strtolower($_REQUEST['textData']);
    echo $articleBody;
    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>";   

}

?>

非常基本的代码,一旦按下&#34;旋转,就什么都没发生!&#34;按钮,我还在学习Ajax,我没有看到任何明显的错误,还有一种方法我可以调试问题是什么,通常php会引发你的错误,任何帮助都是appreaciated!

2 个答案:

答案 0 :(得分:0)

你的Ajax应该是这样的。

$(document).ready(function() { 
    $('#frmAjax').submit(function(e) {
      var text = $('#txtBody').val(); 
      $.ajax({
             url: "ajax-rewriter.php",
             type: "POST",
             data: text,
             success: function(html) {
                $("#mainContent").html(html); //This will load the data at ID "#mainContent".
            }
         });       
    });
});  

答案 1 :(得分:0)

if($_SERVER["REQUEST_METHOD"] == "POST") 

{ 

$data = $_POST['txtbody']  //where txtbody is the name in input form


}