Passing variables to and from PHP

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2005
Posts: 279
Reputation: J_Search is an unknown quantity at this point 
Solved Threads: 6
J_Search J_Search is offline Offline
Posting Whiz in Training

Passing variables to/from JS w/ PHP

 
0
  #1
Jun 7th, 2006
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. //-->")));
Last edited by J_Search; Jun 7th, 2006 at 5:51 pm. Reason: Incorrect title
Attached Files
File Type: zip JavaScript_Rater.zip (21.4 KB, 11 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Passing variables to and from PHP

 
0
  #2
Jun 8th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 279
Reputation: J_Search is an unknown quantity at this point 
Solved Threads: 6
J_Search J_Search is offline Offline
Posting Whiz in Training

Re: Passing variables to and from PHP

 
0
  #3
Jun 10th, 2006
Alright, I'll get right on that. Anyone else?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC