doujing1967 2016-02-26 00:38
浏览 75
已采纳

JQuery表单提交生成一个新表单

I have a JQuery script that submits user input to a PHP script in the same file, and then displays the result of what the PHP script does with the input. That part works fine. The issue that I’m having is that, upon submission, the JQuery script (at least, I think it's the script) also generates a new submission box below the original.

I’m not sure why. I thought at first that it was an issue with the input type, with the asynchronous part, or even with where I had the form in the overall code, but none of those seem to be playing any role. I'm still a beginner and I'm just not seeing the issue.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<form id = "my_form">
verb <input type = "text" id ="word1"/>
<input type = "submit"/></form>
<div id="name"></div>
<script>
$(document).ready(function(){
    $("#my_form").on('submit', function(e)
{
    e.preventDefault();
    var verb = $ ("#word1").val();
    var tag = "#Latin ";
    var url = "http://en.wiktionary.org/wiki/"+verb+tag;
        $.ajax({
            url: "Parser.php",
            data: {"verb": verb},
            type: "POST",
            async: true,
            success: function(result){
                       $("#name").html(result);
                       $("#name").append(url);

                    }
        });
 });
});</script>

RESULT: result of php script

PHP

<?php 

    $bank = array();
    function endsWith($haystack, $needle) {
        return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
    }
    function check_end_array($str, $ends)
    {
       foreach ($ends as $try) {
         if (substr($str, -1*strlen($try))===$try) return $try;
       }
       return false;
    }
    function db_connect() {

        static $connection;

        if(!isset($connection)) {
            $connection = mysqli_connect('127.0.0.1','username','password','Verb_Bank');
        }

        if($connection === false) {
            return mysqli_connect_error(); 
        }
        return $connection;
    }
    function db_query($query) {
        $connection = db_connect();
        $result = mysqli_query($connection,$query);

        return $result;
    } 

    function db_quote($value) {
            $connection = db_connect();
            return "'" . mysqli_real_escape_string($connection,$value) . "'";
        }
    $y = false;
    if (isset($_POST['verb'])){
    $y=db_quote($_POST['verb']);
    echo $y;
    echo "
";

    $m = db_query("SELECT `conjugation` FROM normal_verbs WHERE (" . $y . ")  LIKE CONCAT('%',root,'%')");
    if($m !== false) {
        $rows = array();
        while ($row = mysqli_fetch_assoc($m)) {
            $rows[] = $row;
            }
    }
    foreach ($rows as $key => $value){
        if (in_array("first",$value)==true){
        echo "first conjugation verb
";}
        $y = $_POST["verb"];
        $x = $y;
        foreach ($bank as $key => $value) 
           (series of IF-statements) 
    }}?>
  • 写回答

1条回答 默认 最新

  • douke1942 2016-02-26 01:40
    关注

    As Roamer-1888 says's the problem lies in server side, you are returning a html which has a input too. You need to change your code to return only the result string which you append to the div. Else if this is not possible doing at server side as it might require you to change lot of code, then you can strip off the input element from the result and then append it to the div. Like below.

     success: function(result){
                       var div = document.createElement('div');
                       div.innerHTML = result;
                       $(div).find('input').remove();
    
                       $("#name").html(div.innerHTML);
                       $("#name").append(url);
    
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误