简体   繁体   English

从文本字段中获取文本并使用它更新数据库

[英]grabbing text from a text field and updating db with it

I am trying to grab text from a text field and then use that to update my db with it. 我试图从一个文本字段中获取文本,然后使用它来更新我的数据库。 I have tried using session, get, post and cookie to grab the value using php and this doesn't work. 我尝试使用会话,获取,发布和cookie来使用php来获取值,但这不起作用。 I am new to php so I am thinking this comes out blank because it is server side. 我是php新手,所以我认为这是空白的,因为它是服务器端的。 I did this with javascript and it grabs the value but I am not sure how to use it with php or to update mysql. 我是用javascript完成的,它可以获取值,但是我不确定如何在php中使用它或更新mysql。 any help is appreciated. 任何帮助表示赞赏。 here is some sample code of mine: I have the update on a seperate php page but the input of my text area is not even being grabbed 这是我的一些示例代码:我在单独的php页面上进行了更新,但是我的文本区域的输入甚至都没有被捕获

<?php

function update_db(){
 // echo $_POST["comment"]; 
 $nenabled = $_GET['enabled'];
$comments_new = htmlspecialchars($_GET['comment']);
$nemail = $_GET['email'];

  echo ("<script>console.log('$comments_new')</script>");
?>

<form method ="get" action ="update_user.php">
  <input type="hidden" name = "enabled" value = "nenabled">
  <input type="hidden" name = "comments" value = "comments_new">
  <input type="hidden" name = "email" value = "nemail">

  <input type="submit" >

 <script>

 function doFunction() {

 var com = document.getElementById("comment");
alert(com.value);
 // var comment = '<?php update_db(); ?>';
}

</script>

You have something like :- 你有类似的东西:

<input type="hidden" name = "comments" value = "comments_new">

Then how can you expect 那你怎么能期待

 document.getElementById("comment");

will work? 将工作? You should add the id in your html, like:- 您应该在HTML中添加ID,例如:

 <input type="hidden" id="comment" name = "comments" value = "comments_new">

You won't be able to call a PHP function from inside the javascript function. 您将无法从javascript函数内部调用PHP函数。 Once the page is rendered PHP can no longer modify the page since it is a server side language exclusively. 页面呈现后,PHP不能再修改页面,因为它是服务器端专用语言。 There are two common ways to do this however, the first is to wrap your text input in a form and include a submit button that calls a php file with your code to insert the value into your database. 但是,有两种常见的方法可以做到这一点,第一种是将您的文本输入包装在表单中,并包括一个提交按钮,该按钮使用您的代码调用php文件,以将值插入数据库。 There are plenty of good tutorials out there if you google them but http://html.net/tutorials/php/lesson11.php is a pretty good one. 如果您使用Google搜索,这里有很多不错的教程,但是http://html.net/tutorials/php/lesson11.php是一个非常不错的教程。

The other way, and the way that sounds like it would work best for you, is to use AJAX to make a call to a php function. 另一种方法,听起来像是最适合您的方法,是使用AJAX来调用php函数。 You never navigate away from your page and you can handle success and error conditions. 您永远不会离开页面,并且可以处理成功和错误情况。 AJAX supports GET and POST data to your php file. AJAX支持GET和POST数据到您的php文件。 Again, not reinventing the wheel here is a great AJAX tutorial that spells out using GET and POST, single data, multiple items, return data, etc. http://hayageek.com/jquery-ajax-post/ 再说一次,这里并没有重新发明轮子,而是一个很棒的AJAX教程,该教程使用GET和POST,单个数据,多个项目,返回数据等进行了详细说明。http://hayageek.com/jquery-ajax-post/

Hope that helps! 希望有帮助!

If you want to update your database on submit of the form, then no need to import yout php code within javascript. 如果要在提交表单时更新数据库,则无需在javascript中导入php代码。 You just need to send an AJAX request to your php server on submit your form. 您只需要在提交表单时将AJAX请求发送到您的php服务器即可。 For the AJAX request you can also use jQuery. 对于AJAX请求,您还可以使用jQuery。

At first you've to modify your html form like:- 首先,您必须修改html表单,例如:

<form onsubmit="return false;">
    <input type="hidden" id="name" name = "enabled" value = "nenabled"></input>
    <input type="hidden" id="comments" name = "comments" value = "comments_new"></input>
    <input type="hidden" id="email" name = "email" value = "nemail"></input>
    <input type="button" onclick="update()" value="Submit">
</form>

So, on click of the submit button the update function will be called. 因此,单击提交按钮后,将调用更新功能。 The update function will take the input values and call another function to send the AJAX request. 更新函数将获取输入值,并调用另一个函数来发送AJAX请求。 So I wrote:- 所以我写道:

 <script type="text/javascript">
        function update(){
            var data = {
                name : document.getElementById("name").value,
                comments : document.getElementById("comments").value,
                email : document.getElementById('email').value
            };
            loadXMLDoc(data);

        }

        //Ajax request function
        function loadXMLDoc(data)
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("POST","updateDB.php",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("name="+data.name+"&comments="+data.comments+"&email="+data.email);
        }

    </script>

The AJAX request will send a POST request to AJAX请求将POST请求发送到

your base url + 'updateDB.php' 

with data:- 与数据:-

name:nenabled,
comments:comments_new,
email:nemail,

So, now you can do the rest of the thing in the server side php code to update the database. 因此,现在您可以在服务器端php代码中完成剩下的事情来更新数据库。

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

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