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?

 

 

 

Link to comment
Share on other sites

Ok I figured it out :)

 

$item_name = mysql_escape_String($id); didn't pass the ID name. As for Firefox, I wasn't logged into my site on that browser...

 

I just have one question. Using cache: false, in the Jquery ajax.. is that a good or bad thing?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.