Jump to content

Sending data to server, ID isn't being send.


V

Recommended Posts

I'm having a go at this for hours.. I'm trying to send some data from a link to a script but the "<a href>" id isn't being sent.

 

The js is,

 

$(function() {
$(".add_item").click(function() 
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;

if (name=='new')
{
$.ajax({
   type: "POST",
   url: "add_item.php",
   data: dataString,   
   cache: false,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});
}
}); 
});

 

the code

 

<div class="green_button"><a href="" class="add_item" id="<?php echo $item_name; ?>" name="new">Add Item</a></div>

 

and the script,

 

 

require_once('functions.php');
session_start();

  if (isset($_SESSION['valid_user']))  {

  //create short variable name
  $username = $_SESSION['valid_user'];
  $item_name = $_POST['id'];
  $item_name = mysql_escape_String($id);

  do_html_header('Adding Item');

  $connection = dbConnect();

  $sql = "INSERT INTO user_items (username, item_name, item_date)
          VALUES ('$username', '$item_name', NOW())";
	  
  $result = $connection->query($sql) or die(mysqli_error($connection));

} else {
echo "Access Denied";
}
	  
do_html_footer();

 

It inserts the username and date into the the user_items table but the ID from the link "id=something" isn't being passed, however it shows in the HTML source. I also used firebug to check any js errors and didn't find any. What's even stranger to me, it doesn't work in Firefox, nothing is being added into the DB.

 

I'm not sure what I'm doing wrong.. :facepalm: Can anyone spot a flaw?

 

 

 

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.