Jump to content

Strange results when submittng form


andyd34

Recommended Posts

I have a form that is submitted via jquery into a mysql database using php

 

<form method="post" name="add_loc_form" id="add_loc_form">

    <table width="100%" align="center" cellspacing="0">
      <tr>
        <td class="cat"><h2>Add Location</h2></td>
        <td align="right" class="cat"><a href="#" class="loc_help_a"><img src="{T_THEME_PATH}/images/help.gif" /></a></td>
      </tr>
    </table>
    <div id="loc_help" style="width:95%; display:none; padding:2.5%; background-color:#FFE9D2"> help txt</div>
    <table width="70%" align="center">
      <tr>
        <td valign="top">latitude</td>
        <td valign="top"><input type="text" name="loc_long" id="loc_long" size="20" /></td>
      </tr>
      <tr>
        <td valign="top">Longitude</td>
        <td valign="top"><input type="text" name="loc_lat" id="loc_lat" size="20" /></td>
      </tr>
      <tr>
        <td valign="top">Place name</td>
        <td valign="top"><input type="text" name="loc_place" id="loc_place" size="40" /></td>
      </tr>
      <tr>
        <td valign="top">Description</td>
        <td><textarea cols="30" rows="3" id="loc_desc" name="loc_desc"></textarea></td>
      </tr>
      <tr>
        <td colspan="2" align="center" valign="top"><input type="submit" value="add" name="add_loc" id="add_loc" />
           
          <button class="close_add_loc">close</button></td>
      </tr>
    </table>
  </form>

 

Its is then passed to the jquery

 

$("#add_loc_form").submit(function() {
	var loc_long = $('#loc_long').val(); 
	var loc_lat = $('#loc_lat').val(); 
	var loc_place = $('#loc_place').val(); 
	var loc_desc = $('#loc_desc').val(); 
	var dataString = "loc_long="+ loc_long +"&loc_lat="+ loc_lat +"&loc_place="+ loc_place +"&loc_desc="+ loc_desc;

	if(loc_long=='') {
		$("loc_long").css("border-color:red");
		return false;
	}

	if(loc_lat=='') {
		$("loc_lat").css("border-color:red");
		return false;
	}

	if(loc_place=='') {
		$("loc_place").css("border-color:red");
		return false;
	}

	if(loc_desc=='') {
		$("loc_desc").css("border-color:red");
		return false;
	}


	$.ajax({
		type: "POST",
		url: "./ajax.php",
		data: dataString,
		success: function()
			{
				$('form').clearForm();
				$("#add_map_locations").fadeOut(600);
				$("#map_add_success").fadeIn(600);
			}
	});
	return false;
});

 

the php is

 

		mysql_query("INSERT INTO table_locations 
								(longitude, latitude, place_name, description, user_id) VALUES 
								('$loc_long', '$loc_lat', '$loc_place', '$loc_desc', '$user_id')");

 

The problem is when each entry has a number in it its submitting a rounded number to the database ie loc_long's value is -2.329102 but its submitting -2 for some reason, if you put numbers in the place or description fields its saving as a rounded number as above. If you put text in any of the fields its saving 0 instead of the text. The fields in the database as set to varchar so its not that.

 

Can anyone help please.

Link to comment
https://forums.phpfreaks.com/topic/174134-strange-results-when-submittng-form/
Share on other sites

  `id` int(15) NOT NULL auto_increment,

  `longitude` varchar(15) collate utf8_bin NOT NULL,

  `latitude` varchar(15) collate utf8_bin NOT NULL,

  `place_name` varchar(60) collate utf8_bin NOT NULL,

  `description` varchar(200) collate utf8_bin NOT NULL,

  `user_id` int(15) NOT NULL,

$loc_long = htmlspecialchars($_POST['$loc_long']);

$loc_lat = htmlspecialchars($_POST['$loc_lat']);

$loc_place = htmlspecialchars($_POST['$loc_place']);

$user_id = htmlspecialchars($_POST['''$loc_desc']);

 

mysql_query("INSERT INTO table_locations (longitude, latitude, place_name, description, user_id) VALUES ('$loc_long', '$loc_lat', '$loc_place', '$loc_desc', '$user_id')");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.