Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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/extjs/3.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
Javascript 如何在php表单中添加onkeypress save_Javascript_Php_Ajax - Fatal编程技术网

Javascript 如何在php表单中添加onkeypress save

Javascript 如何在php表单中添加onkeypress save,javascript,php,ajax,Javascript,Php,Ajax,**这是我的phpindex.php文件。我想键入名字和姓氏类型并自动保存数据库。我使用ajax编写了代码。但是,这并不能正常工作。谁能帮我一下吗** index.php <?php $connection = mysql_connect("localhost", "root", ""); $db = mysql_select_db("type", $connection); if(isset($_POST['submit'])){ $firstName = $_POST['first

**这是我的php
index.php
文件。我想键入名字和姓氏类型并自动保存数据库。我使用ajax编写了代码。但是,这并不能正常工作。谁能帮我一下吗**

index.php

<?php
$connection = mysql_connect("localhost", "root", ""); 
$db = mysql_select_db("type", $connection); 
if(isset($_POST['submit'])){
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

if($firstName !=''||$lastName !=''){
//Insert Query of SQL
$query = mysql_query("insert into users(firstName, lastName) values ('$firstName', '$lastName')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysql_close($connection);
?>

<html>
<head>
<meta><title>Home Page</title>
<script type="text/javascript">
$(document).on('keyup','firstName','lastName',function(){
        var rel = $(this).attr('rel');
        var flatvalue = $(this).val();
        $("#firstName"+rel).val(flatvalue);
    });
    </script>
</head>
<body>
    <form action="" method="post"> 
            <label for="firstName">First Name</label>
            <input type="text" name="firstName" ><br><br>
            <label for="lastName">Last Name</label>
            <input type="text" name="lastName" ><br><br>
            <input type="submit" id="submit" name="submit" value="submit"/>
        </form>
</body>
</html>

你试过这样做吗

$('body').delegate('input[type="text"]', 'keypress keydown keyup change propertychange paste', function(event) {
    event.stopImmediatePropagation();

    if (event.type === 'keydown' || event.type === 'keypress') {
        return;
    }
    //insert what you want to do here
    //perform some ajax
    /*
    $.ajax({
        url: 'index.php',
        method: 'POST',
        data: {
            'firstName' : $('input[name=firstName]).val(),
            'lastName' : $('input[name=lastName]).val()
        },
        success: function(mResponse) {
            alert(mResponse);
        }
    });
    */
});
也许这能帮到你, 如果您想通过表单提交来完成,那么只需要在表单的action方法中提供控制器函数的url

<form action="" method="post"> 
        <label for="firstName">First Name</label>
        <input type="text" name="firstName" ><br><br>
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName" ><br><br>
        <input type="submit" id="submit" name="submit" value="submit"/>
    </form>
}))


同样,您可以为输入框赋予class属性,并且在
keyup event
上可以通过class

onKeyPress event保存数据,因为它会根据用户的点击次数多次调用

使用onBlur事件而不是onKeyPress提交表单并将其保存到MySql用户表

试试下面的例子

PHP


发布数据有两种方法。1) 在onKeyUp事件上使用jQuery提交表单。2) 将数据发布到Ajax文件并保存您的数据。请给我帮助。您的意思是每次按下时都要保存文本框上的更改吗?@perseusl,是的,当我在文本框上键入内容时。它想在DBData上保存数据将使用ajax保存,但是,我想知道如何使用onkeypress保存数据,我的朋友。请您帮助我。我对clik事件所做的,您可以通过keypress事件为您的输入框分配一个类属性来完成,因为我附加了id为的click事件,您可以通过class$(“.classname”)来完成。on('keypress',function(){})
$(“.firstName”)。on('keypress',function(){
我做了这样的更改,但这不起作用。数据将使用ajax保存,但是,我想知道如何使用onkeypress保存数据,我的朋友。请您帮助我。按键是什么意思?我提供的代码将保存在KeyPress上。当我键入它时,希望保存在数据库中。在单击
提交
按钮之前,是否要保存点击提交?或者你想让它保存为用户按任意键?我想保存为用户按任意键这是答案。谢谢我的朋友。现在我想自定义它。欢迎总是@loke2。
成功插入数据…!!
此消息如何显示
index.php
设置标题(“Location:index.php?msg=Data Inserted successfully…!!”);并将下面的div添加到index.php中。

这是您已经添加的我的朋友。但我将在URL上显示消息
<form action="" method="post"> 
        <label for="firstName">First Name</label>
        <input type="text" name="firstName" ><br><br>
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName" ><br><br>
        <input type="submit" id="submit" name="submit" value="submit"/>
    </form>
$('#submit').on('click', function(){
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
$.ajax({
        url: url of your controler, //url of your controller function
        type: "POST",
        data: {'first_name' : first_name,'last_name':last_name},
                    success: function (data) {

                        //whatever you want to do on success

                        } else {
                            //in case of no data 
                        }
                    }

                });    
<?php
$connection = mysql_connect("localhost", "jaydeep_mor", "jaydeep_mor"); 
$db = mysql_select_db("jaydeep_mor", $connection);
if(isset($_POST['firstName']) && isset($_POST['lastName'])){
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];

    if($firstName != '' || $lastName != ''){
        //Insert Query of SQL
        $query = mysql_query("insert into users(firstName, lastName) values ('$firstName', '$lastName')");
        header("Location: test.php?msg=Data Inserted successfully...!!");
    }
    else{
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}
mysql_close($connection);
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta><title>Home Page</title>
<script type="text/javascript">
    function saveData(){
        document.forms["userDataForm"].submit();
    }
</script>
</head>
<body>
    <?php if(isset($_GET['msg']) && trim($_GET['msg'])!=""){ ?>
        <br /><div><?php echo $_GET['msg']; ?></div><br />
    <?php } ?>
    <form action="test.php" method="post" name="userDataForm"> 
        <label for="firstName">First Name</label>
        <input type="text" name="firstName" value="<?php echo isset($_POST['firstName'])?$_POST['firstName']:''; ?>" ><br><br>
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName" value="<?php echo isset($_POST['lastName'])?$_POST['lastName']:''; ?>" onblur="return saveData();" ><br><br>
        <!--input type="submit" id="Submit" name="Submit" value="submit"/-->
    </form>
</body>
</html>