简体   繁体   中英

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. I am new to php so I am thinking this comes out blank because it is server side. 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. 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

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:-

 <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. Once the page is rendered PHP can no longer modify the page since it is a server side language exclusively. 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. 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.

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. 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. 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/

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. You just need to send an AJAX request to your php server on submit your form. For the AJAX request you can also use jQuery.

At first you've to modify your html form like:-

<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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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