php session只在本地工作:php,database,ajax

时间:2014-03-24 22:15:21

标签: javascript php ajax database forms

我正在开发一个网站,该网站使用谷歌地图来获取您的浏览器位置,并使用表单来获取其他2个字段。所有四个都提交到数据库。地理定位在所有计算机上都能正常工作,但表单提交会在除了我的每台计算机上产生空结果。在我的网站上,它可以在本地和网站上运行。我已经自学了我所知道的事情,而且我完全被难过了。请帮忙吗?

以下是相关代码:

的index.php:

<?php header('Access-Control-Allow-Origin: *'); ?>
<?php session_start(); ?>


<!DOCTYPE HTML>
<html>
<body>
<form action="" method="post"> 
       <span class="error">#<input type="text" name="id" value="<?php echo $id;?>"></span>
       <span class="error">*</span>
       <br><br>
       <textarea name="comment" rows="5" cols="30">Comment</textarea><br>
       <input type="Submit" name="Submit" value="Submit"> 
       <span class="error"><br> must allow browser geolocation to submit entry</span>
    </form>

    <?php
      if (isset($_POST['Submit'])) 
      { 
        $_SESSION['number'] = $_POST['id'];
        $_SESSION['comment'] = $_POST['comment'];
        session_write_close();
      }
    ?>
<script type="text/javascript">
      function geolocate(){
        // Try HTML5 geolocation
        if(navigator.geolocation){
          navigator.geolocation.getCurrentPosition(function(position){
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var lat = pos.lat();
            var lng = pos.lng();
            ////**********************************************************************************

               $.ajax({
                    type: 'GET',
                    url: 'scripts/submit.php',
                    data: {
                        lat: lat,
                        lng: lng
                    },
                    crossDomain: true,
                dataType: 'jsonp',
                    success: function( data ) {
                        //console.log( data );
                    }//,
                    //beforeSend: setHeader
                });

            ////**********************************************************************************
          }, function() {           //end get current position
            handleNoGeolocation(true);
          });   //end function set to true
        } 
        else { //end if there's geolocation
          // Browser doesn't support Geolocation
          handleNoGeolocation(false);
        }   //end else
      }//end geolocate

      geolocate();
    </script>
</body>
</html>

submit.php:

<?php header('Access-Control-Allow-Origin: *');
session_start();

require("phpsql_dbinfo.php");

$id = $_SESSION['number'];
$comment =$_SESSION['comment'];
$lat = $_GET[lat];
$lng = $_GET[lng];

//database
//OPEN
$con=mysqli_connect("localhost","$username","$password","$database");





// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



    //insert into database
    //$sql="INSERT INTO markers (lat, lng, id, comment, icon) VALUES ('$lat', '$lng', '$id', '$comment', '$icon')";
 $sql="INSERT INTO markers (lat, lng, number, comment) VALUES ('$lat', '$lng', '$id', '$comment')";

    // if problem with query
    if (!mysqli_query($con,$sql))
    {
         die('Error: ' . mysqli_error($con));
    }

//CLOSE
mysqli_close($con);



?>

提交时,表单设置会话变量,然后使用ajax将纬度和经度传递给submit.php。所有都在submit.php中检索。

我无法在任何地方找到我的答案,我没有收到任何错误。提前谢谢!

0 个答案:

没有答案