通过ajax向php页面和mysql表发送php和js变量。

时间:2022-09-26 10:52:43

I have been looking to some similar questions, but none seem to provide the specific answer I am looking for. I have a few js variables, which are being passed through ajax to a location.php, which will then put them in a mysql table. So far the ajax call looks like this:

我一直在寻找一些类似的问题,但似乎没有一个能提供我正在寻找的具体答案。我有几个js变量,它们通过ajax传递到一个位置。php,然后把它们放在mysql表中。到目前为止,ajax调用看起来是这样的:

function postLocation()
{
  //var teamName = $('.team_name').val(); //Neither of the two work
	var teamName = document.getElementById('team_name').value;
  navigator.geolocation.getCurrentPosition(function(position) 
  {
    pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    $.ajax(
    {
      url:'location.php',
      type: 'POST',
      datatype: 'json',
      data: {
        'posx':position.coords.latitude, 
        'posy': position.coords.longitude, 
        'team_name':$team_name
      },   // post to location.php
      success: function(data) {
        alert(data);
      },

      error: function(data) {
        alert("There may an error on uploading. Try again later"); //This is what displays on the screen every time
      },
    });
  });    
}

You'll notice as well that I have 'team_name' = $team_name -> this is essentially what I want to do, but I cannot find the best way to do this using my existing ajax structure.

您还会注意到,我有'team_name' = $team_name ->这就是我想要做的,但是我无法找到使用现有ajax结构的最佳方式。

EDIT using var teamName = document.getElementById('team_name').value; to get the value from a hidden field just causes the alert to give the error message. In the browser debug it tells me it's a 500 internal server error.

使用var teamName = document.getElementById('team_name').value进行编辑;要从隐藏字段中获取值,只会导致警报给出错误消息。在浏览器调试中,它告诉我这是一个500个内部服务器错误。

EDIT 2 Here is my html code on the index.php page:

编辑2这是我在索引上的html代码。php页面:

<div class="titleText"><h1><?php 
	echo $stagerow['stage'];
	echo $id;
	$team_name = $_SESSION['team_name']; 
	echo $team_name;
	?></h1>
	You can <a href="http://testphp-olivlaytchev.rhcloud.com/logout.php" style="color:white">logout here.</a> <br></div>
		<div id="map-canvas"></div>
  </head>

	 <input type ="hidden" id="team_name" name="team_name" value="<?php echo $team_name?>" >
  <!-- Script -->
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
   <script>

And this is the error message I get from the browser POST http://testphp-olivlaytchev.rhcloud.com/location.php 500 (Internal Server Error)send @ jquery.js:9664m.extend.ajax @ jquery.js:9215(anonymous function) @ (index):259

这是我从浏览器POST http://testphp-olivlaytchev. rhcloud.com/loc.php 500(内部服务器错误)中得到的错误消息,发送@ jquery.js:9664m.extend。ajax @ jquery。js:9215(匿名函数)@(指数):259

line 259 is $.ajax(

259行是$ . ajax(

1 个解决方案

#1


3  

if $team_name is a php variable, you can echo it out into a hidden input and grab its val via js

如果$team_name是一个php变量,您可以将它回显为一个隐藏的输入,并通过js获取它的val

<input type ="hidden" class="team_name" name="team_name" value="<?php echo $team_name?>" >

You have an error at the section when you pass data in your ajax. 'team_name':$team_name should be team_name: teamName`.

在ajax中传递数据时,在该节中会出现错误。'team_name':$team_name应该是team_name: teamName '。

The first arg is passed to HTTP_POST as key index, the second is the value and you don't need $ sign.

第一个arg作为键索引传递给HTTP_POST,第二个是值,不需要$ sign。

var teamName = $('.team_name').val();
data: {
    'posx':position.coords.latitude, 
    'posy': position.coords.longitude, 
     team_name: teamName 
  },   /

in your php you can access it like below.

在php中,可以像下面这样访问它。

echo $_POST['data'];

#1


3  

if $team_name is a php variable, you can echo it out into a hidden input and grab its val via js

如果$team_name是一个php变量,您可以将它回显为一个隐藏的输入,并通过js获取它的val

<input type ="hidden" class="team_name" name="team_name" value="<?php echo $team_name?>" >

You have an error at the section when you pass data in your ajax. 'team_name':$team_name should be team_name: teamName`.

在ajax中传递数据时,在该节中会出现错误。'team_name':$team_name应该是team_name: teamName '。

The first arg is passed to HTTP_POST as key index, the second is the value and you don't need $ sign.

第一个arg作为键索引传递给HTTP_POST,第二个是值,不需要$ sign。

var teamName = $('.team_name').val();
data: {
    'posx':position.coords.latitude, 
    'posy': position.coords.longitude, 
     team_name: teamName 
  },   /

in your php you can access it like below.

在php中,可以像下面这样访问它。

echo $_POST['data'];