1

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:

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.

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.

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

<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

line 259 is $.ajax(

8
  • 1
    Where does $team_name come from? Is it a php variable? Is it global JS variable? What is its scope? Commented Aug 25, 2015 at 13:49
  • From where you get varaible $team_name? Commented Aug 25, 2015 at 13:50
  • 'team_name':'<?PHP echo $team_name; ?>' Commented Aug 25, 2015 at 13:52
  • team_name is a '$_SESSION' variable Commented Aug 25, 2015 at 14:00
  • @mplungjan that doesn't work - tried it already Commented Aug 25, 2015 at 14:01

1 Answer 1

3

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

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

The first arg is passed to HTTP_POST as key index, the second is the value and you don't need $ 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.

echo $_POST['data'];
Sign up to request clarification or add additional context in comments.

6 Comments

or directly into the script if it is inline
it must be failing, because I am getting the error message in the case of error: function(data)
@Oliver add html, update your code and show full error message please. If will help answer your question and find errors quickly and accurately.
var teamName = $('.team_name').val() you are trying to get the val by class, your input does not have an attr class defined add class="team_name". inspect element and check the value echoed out? what do you see?
if I use var teamName = $('.team_name').val() and output the value of teamName - it works correctly and has the correct teamname, however the issue must be in the data stream??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.