3

I am trying to insert javascript varaible to php mysql, but it is not inserting. it is inserting as <javascript>document.write(window.outerWidth); </javascript> x <javascript>document.write(window.outerHeight); </javascript>. but the result is 1366 x 728

What should I do?

<?php
$width = " <script>document.write(window.outerWidth); </script>";
$height = " <script>document.write(window.outerHeight); </script>";
$xex = " x ";
$resulteee = "$width $xex $height";
echo $resulteee;
?>
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
only chande
  • 99
  • 2
  • 10
  • JavaScript runs on the client, *after* it parses the file HTML. There is thus *no* JavaScript shown; just some strings that contain some " – user2864740 Aug 22 '15 at 05:52
  • you need to use `XMLHttpRequest` i.e ajax. – Jigar Aug 22 '15 at 05:52
  • ok sir, what should i do now to insert the result 1366 x 728 in my php mysql database. actually i am using this code get visitors browser size. – only chande Aug 22 '15 at 05:53
  • http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – user2864740 Aug 22 '15 at 05:55
  • thank you user2864740 sir, but unfortunately i am new to php, i didnt understand the code in the link u have given to me sir. – only chande Aug 22 '15 at 06:01
  • Javascript is on the client side. PHP is on the server side. If you want to pass data from the client to the server. In a browser context you need to use AJAX. – severin.julien Aug 22 '15 at 06:40

4 Answers4

2

AJAX is a good solution to your problem :

<script type="text/javascript">
     function call_ajax () {
           var width = window.outerWidth;
           var height = window.outerHeight;

           var xmlhttp = new XMLHttpRequest();

           xmlhttp.onreadystatechange = function() {
                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                     document.getElementById("abc").innerHTML = xmlhttp.responseText;
                 }
             }
             xmlhttp.open("POST", "a.php?height="+height+"width="+width, true);
             xmlhttp.send();
        }
    </script>

and on the page a.php, you can echo your variables to get the output like this :

<?php
echo $_POST['height'];
echo $_POST['width'];
die;
Nehal
  • 1,542
  • 4
  • 17
  • 30
  • where you can make a div with id "abc" on the same page from where you are calling the function. So the output will be shown in that div.
    – Nehal Aug 22 '15 at 06:50
  • i have to use this above codes in two different pages sir? – only chande Aug 22 '15 at 06:52
  • Yes, as you are using ajax. You need to send your parameters to a different page. and those values can be fetched as response on the same page where you used and called your function. – Nehal Aug 22 '15 at 06:57
  • i tried, it is not working sir, i save ajax code as code.php and the below one as a.php file. and i tried to open both code.php and a.php. it is showing blank page sir. – only chande Aug 22 '15 at 07:00
  • Obviously, it will show a blank page only. You need to add some HTML code there, then only something will be shown on that page. – Nehal Aug 22 '15 at 07:44
  • what html code i have to add sir, and in which page code.php or a.php – only chande Aug 22 '15 at 08:41
  • on the code.php and then run it on the browser. What actually you want to do if you can specify. And to inform you I'm female – Nehal Aug 22 '15 at 08:53
  • madam, i have run code.php by adding html code. i am getting blank screen. – only chande Aug 22 '15 at 09:00
  • if you can show me your code, what you have done and what you want to be done. Then I can help you surely – Nehal Aug 22 '15 at 11:02
1

The best way is AJAX, which is a way for Javascript to send data to a PHP script. You should do some research on your own, but your solution will end up looking something like this. I'm using jQuery syntax, which is a really helpful Javascript library that I recommend looking into.

// get values we want
var width = window.outerWidth;
var height = window.outerHeight;
var payload = {"width" : width, "height" : height}; // just a normal object

// send them to server
$.get('/path/to/script.php', payload, function(response) {
    alert('Sent the values!');
});

And in your PHP:

<?php
$width = $_GET['width'];
$height = $_GET['height];

/*
 * DEFINITELY sanitize these things before they're anywhere NEAR the database!
 * research "prepared statements" and "mysqli escape" or you are going to have a very bad time with a hacked server
 */

// do some database stuff!

Hopefully this gives you a good starting point. You really need to make sure you sanitize data before you blindly let it touch a database query or attackers can easily perform a SQL Injection attack, deleting your database or dumping all your data. These are very bad things.

Jeff
  • 789
  • 4
  • 13
  • thank you sir, i will try this code – only chande Aug 22 '15 at 06:42
  • This won't work out of the box - note that the AJAX request is literally submitting to `/path/to/script.php`. You'll need to tweak that to your proper URL and expect the data in those two `$_GET` variables! – Jeff Aug 22 '15 at 06:43
1

You'll have to send it to a separate php file to insert it into MySQL... You'll also have to use Ajax. Include the jquery plugin in your page for that.

So this would include this in your main page. Call the submitstuff() function when the button is pushed instead of submitting a form like normal:

<script>
function submitstuff(){
    var wheight = window.outerHeight;
    var wwidth = window.outerWidth;
    var results = wwidth+" x "+wheight; 
    $.ajax({
    url : "submit.php",
    type: "POST",
    data : "result="+results,
    });
}
</script>

Then, make a file called submit.php and put it in the same folder as your main file.

submit.php

/* include all your database connection stuff */

mysql_query("insert into `yourtable` (`size`) values ('".$_POST['result']."');");

I didn't test this, but I think it might work... :)

Jeremy Board
  • 181
  • 1
  • 9
1

Try jQuery's $.post

var width  = x;
var height = y;

$.post( "page.php",      // name of the page you want to send the variables
    {width:width,height:height},       // variables
    function( data ) {                 // returned values from the page
        alert(data);
    }
);

You can get the variables using $_POST['width'] and $_POST['height'].

Matthias A. Eckhart
  • 5,136
  • 4
  • 27
  • 34
Aneesh Kumar
  • 903
  • 1
  • 6
  • 10