-2

How can I save info that I have in a div named '#time' to a database?

<div id="time" style="float:right;font-size:15px;">0:00:00</div>

The code above is a jq count up timer. What I'm trying is to save every tick in the database. How can I do this?

$.ajax({
                    type: "POST",
                    url: "setupcounter.php",
                    data: {action: 'save',
                    field: $("#time").val(),},
                    success: function(msg){

                    }
                    error: function(){
                        alert('error');
                    }
                });
Roy
  • 1
  • 4
  • edited the post with the ajax i tried, i dont know what i need to write in the php file. – Roy Feb 01 '16 at 07:56
  • `$_POST[ 'field' ]` holds the data you are looking for. How to save an entry to a database is an entirely different subject. You should at least try something for yourself first. There's many tutorials to find on the subject. – Rein Feb 01 '16 at 08:00
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Rein Feb 01 '16 at 08:01
  • @rein i know how to save it to the database, just asking how do i save it as a variable. – Roy Feb 01 '16 at 08:12
  • Where exactly does it go wrong? The data can be accessed from within your javascript by: `$("#time").val()`, once you made a successful post to your PHP script, you can access the posted data by using `$_POST['field']`. – Rein Feb 01 '16 at 08:19
  • Thats the php file code: `` all I tried was to echo it to check but i get undefined index. – Roy Feb 01 '16 at 08:20
  • on the chrome network i can see that the post was recived and i can see the correct time on the preview, but on the page I cant. – Roy Feb 01 '16 at 08:21
  • How about you actually echo what I told you to: `` – Rein Feb 01 '16 at 08:21
  • I tried it before and got the same error – Roy Feb 01 '16 at 08:22
  • `Notice: Undefined index: field in C:\xampp\htdocs\comp\setupcounter.php on line 1` – Roy Feb 01 '16 at 08:22

2 Answers2

1

Actually you want to get text of div. So do this..

Just replace

$("#time").val()

with

$("#time").text()
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
0
        var time = $('#time').val();
        //Just add whatever params you need to the datastring variable.
         var dataString = 'time='+time+'&action=save';
        //Check console that you are sending the right data
        console.log(dataString) 
        $.ajax({
             type: "POST",
             url: "setupcounter.php",
             data: dataString,
             success: function(msg){
              console.log(msg);
              }
     });

In php you would do:

$time = $_POST['time'];
$action = $_POST['action'];
//Db stuff

Fiddle

Havihavi
  • 652
  • 1
  • 9
  • 26
  • and i get on the php page undefined index again. – Roy Feb 01 '16 at 14:43
  • There's not really anything wrong with this. updated it and added a fiddle. mind you, theres no real .php to test it with in jsfiddle. i use this snippet all the time. – Havihavi Feb 02 '16 at 10:37