Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交按钮在没有刷新AJAX的情况下可以工作吗?_Php_Javascript_Ajax - Fatal编程技术网

Php 提交按钮在没有刷新AJAX的情况下可以工作吗?

Php 提交按钮在没有刷新AJAX的情况下可以工作吗?,php,javascript,ajax,Php,Javascript,Ajax,我的简单聊天有问题。使用AJAX不断刷新页面,超时时间为750ms。如果我按enter键或使用enter键提交我的“反应”,页面将刷新:是否有办法删除该选项,以便您可以立即看到您发布的内容 您可以在我的网站上看到聊天: 代码: Index.php <!DOCTYPE HTML> <?php include 'config.php'; ?> <html> <head> <script type="text/javascript" src="jq

我的简单聊天有问题。使用AJAX不断刷新页面,超时时间为750ms。如果我按enter键或使用enter键提交我的“反应”,页面将刷新:是否有办法删除该选项,以便您可以立即看到您发布的内容

您可以在我的网站上看到聊天:

代码:
Index.php

<!DOCTYPE HTML>
<?php include 'config.php'; ?>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.js">
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}


</script>

    <title>JavaScript Chat</title>
    <link href="style.css" rel="stylesheet" type="text/css"/>

</head>
<body>
    <div class="container">
        <div id="chatwindow">

        </div>

        <div class="inputMessage">
            <form method="post">


    enter code here

            <hr></hr>
                <textarea name="message" rows="1" cols="55"></textarea><br/>Fill username here<br/>
                <input type="text" value="" name="username" />
                <input type="submit" value="verstuur" name="submit" onKeyPress="return submitenter(this,event)" />

            </form>
            <?php   include 'send.php'; ?>      
        </div>

        <script type="text/javascript">
          $(document).ready(function(){
            setInterval ( "get()", 750 );
          });
          function get(){
             $.ajax({
               type: 'GET',
               url: 'chat.php',
               success: function(data){
                 $("#chatwindow").html(data);
               }
             });
          }
        </script>
</div>
</body>
</html>

功能提交人(myfield,e)
{
var键码;
如果(window.event)keycode=window.event.keycode;
如果(e)keycode=e,则为else;
否则返回true;
如果(键代码==13)
{
myfield.form.submit();
返回false;
}
其他的
返回true;
}
JavaScript聊天
在这里输入代码


在此处填写用户名
$(文档).ready(函数(){ setInterval(“get()”,750); }); 函数get(){ $.ajax({ 键入:“GET”, url:'chat.php', 成功:功能(数据){ $(“#聊天窗口”).html(数据); } }); }
chat.php

<?php
    include 'config.php';
    $result = mysql_query("select * from Message");
    while($row = mysql_fetch_array($result))
    {
        echo '<p>' . $row['username'] . " : " . $row['message'] . '</p>';
    }
?>  

send.php

<?php 

if(isset($_POST['submit'])) 
    {
    if (!empty($_POST['username']))
        {
            if(!empty($_POST['message']))
            {
                $message =  mysql_real_escape_string(htmlentities($_POST['message']));
                $username =  mysql_real_escape_string(htmlentities($_POST['username']));
                $query = "INSERT INTO Message (`username`,`message`) VALUES ('".$username."','".$message."')";
                mysql_query($query);
            }
            else
            {
            echo '<script type="text/javascript">alert(\'Je kan niet niks sturen\')</script>'; 
            }
        }
    else
    {
        echo '<script type="text/javascript">alert(\'Vul een gebruikresnaam in!\')</script>';
    }
}
?>

如果我的问题不清楚,请说出来。 是否有关于良好间距的主题/问题/帖子?谷歌将其翻译为“缩进”。 谢谢

添加
e.preventDefault()在JS中。

添加
e.preventDefault()在JS中。

替换

<form method="post">


您也可以在此处使用回调函数,如:

 <form onsubmit="event.preventDefault();return submitenter(this,event);" method="post">

工作演示:

更换

<form method="post">


您也可以在此处使用回调函数,如:

 <form onsubmit="event.preventDefault();return submitenter(this,event);" method="post">


工作演示:

您希望采取的措施是防止其他答案提到的提交操作。目前您的脚本还没有完全准备好阻止提交,因为您没有ajax post方法

应用程序的提交端仍然需要ajax功能。为此,您可以使用

您想创建类似于

function send() {
    $.post(); // Fill in appropriate post() code.
    return false;
}

然后从事件处理程序(如
onsubmit=“return send()”
)调用它,并替代按键处理程序中的
myfield.form.submit()

您所需的操作是阻止onsubmit操作,正如其他答案所述。目前您的脚本还没有完全准备好阻止提交,因为您没有ajax post方法

应用程序的提交端仍然需要ajax功能。为此,您可以使用

您想创建类似于

function send() {
    $.post(); // Fill in appropriate post() code.
    return false;
}

然后从事件处理程序调用它,如
onsubmit=“return send()”
,并替换按键处理程序中的
myfield.form.submit()

谢谢,但如果我将我的答案更改为youre,它将完全停止工作。我应该添加一些js吗?在callcack函数中执行您想要执行的操作
我在firebug中遇到错误:在编辑后,Submitter未定义。仅举一个例子。我认为你必须学习JS基础知识才能自己完成这项工作。您的
标签甚至都不正确。谢谢,我想这是我的问题。也许我会很快,你知道一个好的网站吗?还是我去学校学基础知识?谢谢,但如果我改成你的答案,它就完全不起作用了。我应该添加一些js吗?在callcack函数中执行您想要执行的操作
我在firebug中遇到错误:在编辑后,Submitter未定义。仅举一个例子。我认为你必须学习JS基础知识才能自己完成这项工作。您的
标签甚至都不正确。谢谢,我想这是我的问题。也许我会很快,你知道一个好的网站吗?还是我去学校学习基础知识?谢谢你,我以后再看!谢谢你,我以后再看!