0

I want to get the value of one text box if a particular button is clicked. I can't send the value through url so I tried using javascript. Here is my php code:

<a href="yahoo/Connected.php">
<input type='button' class='button' id='yahoo' value='Yahoo'></a>
<p>Your name, emails will be sent with your name in Subject</p>
<input type='text' class='inviteename' id='text' name='name'>

Here is my javascript code:

<script>
$('#yahoo').live('click',function() {
var inviteename = $('.inviteename').val();
function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}
createCookie('invitee_name',+inviteename,'1');
});
</script>

Through this, cookie is not being create, can anyone spot what wrong I'm doing?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Guy in the chair
  • 1,045
  • 1
  • 13
  • 42

2 Answers2

1
  1. your html is invalid. Why wrap input fields in a link?

  2. .live is deprecated, you should use .on - I use on submit here.

  3. you should use a cookie jQuery plugin instead of wrapping a named function into your click.

Live Demo

Please note the demo is using "p" instead of "name" to actualy do something. Hit F12, Run the demo once then reload to see the cookie was set

<form id='yahoo'  action="yahoo/Connected.php">
<input type='submit' class='button' value='Yahoo'></a>
<p>Your name, emails will be sent with your name in Subject</p>
<input type='text' class='inviteename' id='text' name='name'>
</form>

using

$(function() {
  $('#yahoo').on('submit',function(e) {
    var inviteename = $('.inviteename').val();
    $.cookie('invitee_name',inviteename);
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I appreciate the advice, but this doesn't resolve my problem, cookie not being created. – Guy in the chair Mar 21 '14 at 07:37
  • You need to install https://github.com/carhartl/jquery-cookie - more information [here](http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery). Also you had a + in the invitee name - not needed unless you want to convert a string containing a number to a number before it is stored as a string – mplungjan Mar 21 '14 at 07:40
  • There is no link for demo, can you post it again? And with removing '(' it doesn't work as well. – Guy in the chair Mar 21 '14 at 10:06
  • It seems that just doesn't work on my server, I will look something to it. Example's working fine. – Guy in the chair Mar 21 '14 at 10:22
  • Are you looking at the cookie in the same folder or on a different page in a different folder? – mplungjan Mar 21 '14 at 10:30
  • I'm looking at the developer tools only whether cookie has been set. – Guy in the chair Mar 21 '14 at 10:40
  • So `console.log($.cookie("invitee_name"))` shows undefined? Are you running from a server? Is the browser set to accept cookies from that server? You cannot set a cookie from a file loaded from harddisk – mplungjan Mar 21 '14 at 10:43
  • That doesn't display anything & yes cookies are well accepted, in fact this is the live server I'm playing with. I've many more cookies already running, this one is first in javascript. – Guy in the chair Mar 21 '14 at 10:46
  • It should display EITHER undefined OR the value - not nothing - can you post your code in a fiddle that shows it doing nothing? You can fork mine to get the frameworks and plugins – mplungjan Mar 21 '14 at 12:18
  • 1
    It's done, thanks a lot buddy, problem was in other file as I was including foundation js file once this cookie.js file is loaded. Loaded it at the last & it worked like a charm. It was seriously hell of an issue, but a sweet fruit at the end :) – Guy in the chair Mar 21 '14 at 18:05
0

Try this little script, to check or creating cookies actually works ...

<?php
    if (!isset($_COOKIE['MyCookie'])) {
    setcookie ('MyCookie', 'yes', time() + 3600);
    echo "cookie is CREATED";
    }
    else{
    echo "cookie EXIST";
    }
?>
Danny
  • 346
  • 1
  • 10