943,695 Members | Top Members by Rank

Ad:
Jun 7th, 2006
0

Passing variables to/from JS w/ PHP

Expand Post »
All,

Here is some JavaScript I found. It creates a small scrollbar, which allows a user to scroll though a series of 5 images. The images represent votes, and the user can then press a "Rate us" button in order to cast a vote. As you can see in the code, followed by the images loading section, the default image/rating is set at 5. I would like to dynamically change this with a value stored in MySQL by passing the variable via PHP. I would again like to pass the new variable (the image/rating the user chose before selecting the submit button) to MySQL via PHP again. I'm not sure if this is even possible, but I thought I'd ask. I don't know JavaScript, so I'm sorry if this a noob question, cause I am. I do know PHP and MySQL though.

I'm open to other options as well. But I really like the idea of a small scrollbar and the ability to scroll through a series of images and cast a vote based on the image chosen. Could this be done in Flash? If JavaScripts work, I'd rather use that, cause I've got all the code right in front of me. .... and now you.

Here is the code. I've also zipped and attached a working version. Just unzip the folder and htm file, and run the index.htm file.

Thanks!!!

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var set_action = "";
  2. var set_path = "images/";
  3. var set_id="<!--%ID%-->";
  4. var set_spacer = "spacer.gif";
  5. var set_scroll = "f0.gif";
  6. var set_thumb = "f1.gif";
  7. var set_rate1 = "1.gif";
  8. var set_rate2 = "25.gif";
  9. var set_rate3 = "5.gif";
  10. var set_rate4 = "75.gif";
  11. var set_rate5 = "10.gif";
  12. var set_mark1 = "1";
  13. var set_mark2 = "2";
  14. var set_mark3 = "3";
  15. var set_mark4 = "4";
  16. var set_mark5 = "5";
  17. var set_text = "";
  18. var set_submit = "Rate us";
  19. //SETTINGS END
  20. var gotkl = 0;
  21. var path = set_path;
  22. var index = 0;
  23. var firstTime = true;
  24. var img_f = new Array();
  25. img_f[0] = new Image();
  26. img_f[1] = new Image();
  27. img_f[0].src= path + set_scroll;
  28. img_f[1].src= path + set_thumb;
  29. //Files with images
  30. var img_p = new Array();
  31. img_p[1] = new Image();
  32. img_p[2] = new Image();
  33. img_p[3] = new Image();
  34. img_p[4] = new Image();
  35. img_p[5] = new Image();
  36. set_spacer =path + set_spacer;
  37. img_p[1].src=path + set_rate1
  38. img_p[2].src=path + set_rate2
  39. img_p[3].src=path + set_rate3
  40. img_p[4].src=path + set_rate4
  41. img_p[5].src=path + set_rate5
  42. function imgonload()
  43. {
  44. // I need to be able to set this value dynamically. Currently it is statically set to 5.
  45. document.id_pn.src = img_p[5].src;
  46. }
  47. flg = (document.all) ? 0 : 1;
  48. var obj;
  49. var dx,dy;
  50. var flag = false;
  51. function mousedown(ev)
  52. {
  53. if (flg)
  54. {
  55. X=ev.clientX;
  56. Y=ev.clientY;
  57. return false;
  58. }
  59. else
  60. {
  61. X=event.clientX;
  62. Y=event.clientY;
  63. }
  64. }
  65. function mousemove(ev)
  66. {
  67. if (flag)
  68. {
  69.  
  70. if (flg)
  71. {
  72. X2=ev.clientX;
  73. Y2=ev.clientY;
  74. }
  75. else
  76. {
  77. X2=event.clientX;
  78. Y2=event.clientY;
  79. }
  80. dy=Y2-Y;
  81. var dh=document.id_spacer.height+dy;
  82.  
  83. if (dh>-1 && document.id_spacer.height+dy<document.id_pn.height-7) {
  84.  
  85. document.id_spacer.height = document.id_spacer.height+(Y2-Y);
  86. Y=Y2;
  87. };
  88. if (dh<=-1)
  89. {
  90. document.id_spacer.height=1;
  91. }
  92. if (dh>=document.id_pn.height-7)
  93. {
  94. document.id_spacer.height=document.id_pn.height-8;
  95. }
  96. var mark=Math.floor(document.id_spacer.height/(document.id_pn.height/5));
  97. //alert(mark);
  98. mark=5-mark;
  99. document.id_pn.src = img_p[mark].src
  100. document.post_form.rate.value=mark;
  101. //window.status=mark;
  102. return false;
  103. }
  104. }
  105. function mouseup()
  106. {
  107. obj = null;
  108. flag = false;
  109. }
  110. if (flg)
  111. {
  112. document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
  113. }
  114. document.onmousedown = mousedown;
  115. document.onmousemove = mousemove;
  116. document.onmouseup = mouseup;
  117. document.write(""+
  118. "<table border='0' cellspacing='0' cellpadding='0' width='100px'>"+
  119. " <tr>"+
  120. " <td align='center'>"+
  121. " <img align='middle' name='id_pn'>"+
  122. " </td>"+
  123. " <td id='scroll_cell' align='center' valign='top' width='20' background='"+img_f[0].src+"'>"+
  124. "<table border=0 cellspacing='0' cellpadding='0'><tr><td><img border=0 name='id_spacer' width='20' height='0' src='"+set_spacer+"'></td></tr><tr><td><img width='20' height='8' border='0' id='id_pil' src='"+img_f[1].src+" 'style='cursor : hand;' onMouseDown='flag = true;' ></td></table>"+
  125. " </td>"+
  126. " </tr>"+
  127. "<form action='"+set_action+"' method='post' name='post_form'>"+
  128. " <tr>"+
  129. " <td colspan='2' style='padding: 2px' align='left'>"+
  130. " <input style='width:80%' type='submit' value='"+set_submit+"'>"+
  131. " <input type='hidden' name='ID' value='"+set_id+"'><input type='hidden' name='rate' value='5'>"+
  132. " </td>"+
  133. " </tr></form></table>");
  134. imgonload();
  135. //-->")));
Attached Files
File Type: zip JavaScript_Rater.zip (21.4 KB, 40 views)
Last edited by J_Search; Jun 7th, 2006 at 5:51 pm. Reason: Incorrect title
Similar Threads
Reputation Points: 27
Solved Threads: 6
Posting Whiz in Training
J_Search is offline Offline
284 posts
since Aug 2005
Jun 8th, 2006
0

Re: Passing variables to and from PHP

You cannot pass variables, per se, between JavaScript and PHP. However, your PHP script can certainly author JavaScript, including JavaScript variables. I hope that makes sense, because it's late and I don't feel like debugging all that code you posted. Thanks for using code tags, though!

Seriously, with your JavaScript, use PHP to simpy "echo" the JavaScript variable.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jun 10th, 2006
0

Re: Passing variables to and from PHP

Alright, I'll get right on that. Anyone else?
Reputation Points: 27
Solved Threads: 6
Posting Whiz in Training
J_Search is offline Offline
284 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Processing Checkbox Information From A Form
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Where do you get this WYSIWYG editor on DaniWeb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC