axl8910 Posted July 4, 2011 Share Posted July 4, 2011 Hi, I am trying to implement ajax in my code but I fail and I can't get it to work. I am very new at this so I would like your help. I am trying to assign flexi with buttonclick to user that is currently logged in so my table in HTML and mysql both changes. It works in HTML, it changes OK but mysql updates as soon as I get to the webpage and it's supposed to change onclick. I tried to echo $q but it says undefined *feeling stupid* index.php <?php session_start(); ?> <html> <head> <script type="text/javascript"> var xmlHttp; function showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="getuser.php" url=url+"?q="+str xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function change1() { var first = "<?php echo $_SESSION['username']; ?>"; var x=document.getElementById("mytable").rows var y=x[1].cells y[3].innerHTML=first; y[2].innerHTML="allocated"; } function change2() { var second= "<?php echo $_SESSION['username']; ?>"; var x=document.getElementById("mytable").rows var y=x[2].cells y[3].innerHTML=second; y[2].innerHTML="allocated"; } </script> </head> <body onload='showUser(this.value)'> <form> <input type='button' id='button1' onclick="change1(this.form)" value='Allocate'> <input type='button' id='button2' onclick="change2(this.form)" value='Allocate'> </form> <div id="txtHint"></div> </body> </html> getuser.php <?php session_start(); require_once("connect.php"); $q=$_GET["q"]; $sql="SELECT * FROM flexi"; $username=$_SESSION['username']; $result = mysql_query($sql); echo $q; if($q=1) { $query1 = 'UPDATE flexi SET allocation = "allocated", user = "'.$_SESSION["username"].'" WHERE flexi="flexi1"'; $result1 = mysql_query($query1); $result1 = $_SESSION["allocate"]; }else{} if($q=2) { $query2 = 'UPDATE flexi SET allocation = "allocated", user = "'.$_SESSION["username"].'" WHERE flexi="flexi2"'; $result2 = mysql_query($query2); $result2 = $_SESSION["alloc"]; }else{} echo "<html> <body><table border='1' id='mytable'> <tr> <th>Flexi</th> <th>Status</th> <th>Allocation</th> <th>User</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['flexi'] . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['allocation'] . "</td>"; echo "<td>" . $row['user'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/ Share on other sites More sharing options...
sunfighter Posted July 4, 2011 Share Posted July 4, 2011 Your HTML is good. Works with another php script. Your PHP script is bad. Has two occurrences of }else{} If fixed the last else has no closing bracket. The </body> </html> at the end shouldn't be. Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1238280 Share on other sites More sharing options...
axl8910 Posted July 5, 2011 Author Share Posted July 5, 2011 I fixed it and still my variable is undefined: $q=$_GET["q"]; echo $q; Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1238429 Share on other sites More sharing options...
sunfighter Posted July 8, 2011 Share Posted July 8, 2011 Because this is posted under Ajax help I just check the ajax portion. Then upon seeing errors in the php I had you fix those thing it would fix your problem, but the problem is with the html. Sorry. :'( This line: <body onload='showUser(this.value)'> this.value is undefined. What are you trying to send? In a practical application you can execute the php code at the top of the page without using ajax so I'm thinking this is just an exercise. Right? Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1239927 Share on other sites More sharing options...
axl8910 Posted July 8, 2011 Author Share Posted July 8, 2011 Yes... I am student on co-op never worked with web programming before.. and I am trying to learn it... sry for being noob. I don't want my page to refresh that's why I need to use ajax isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1239967 Share on other sites More sharing options...
sunfighter Posted July 8, 2011 Share Posted July 8, 2011 No need to apologize for being new, we all were at one time. Some of us are not that far removed. Me for example. What I said was - calling ajax on page load is ( I was told to say 'inefficient'). What you're calling on ajax to do can be done when loading the page, just put the code in php tags and use the .php extension. What are you trying to do? PS - No, ajax is not the only way to change your page without reloading. Javascript can do that by it's self. Normally ajax would be used when you needed to access a database and then display the results, easiest example. Definitely not changing the page with calling ajax with onload. Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1240223 Share on other sites More sharing options...
axl8910 Posted July 9, 2011 Author Share Posted July 9, 2011 ok=) so... user gets in page where he needs to login, if it is successful it redirects him to page where I have table which is same as table in MySql. In table I have flexiname, status(if it is on or off), allocation(allocated/deallocated), and user who is currently using flexi, if it is not using user in "none". So near that table I have buttons for each flexi. When I want to use and take flexi1 for example I see it is deallocated and user is none, so I can take it. When I click on button that flexi should be allocated to me, so fields in HTML table AND in MySql should be updated to allocation "allocated" and user "ana" for example. My problem is that it works great in HTML table I click on each button and it works for each flexi, but MySql table doesn't update good. As soon as I get into page after login all flexi in table get allocated and user is "ana" for each flexi without clicking button or anything. I want to do is stop MySql from updating when I enter the page and update it when I click on button just like in HTML. Hope I explained it clearly=) Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1240440 Share on other sites More sharing options...
axl8910 Posted July 11, 2011 Author Share Posted July 11, 2011 nothing? Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1241217 Share on other sites More sharing options...
sunfighter Posted July 14, 2011 Share Posted July 14, 2011 Sorry, Haven't been on in a while. What is 'flexi' ? I have no idea. You control mysql. I do not understand why you have a html table. You should download this info from mysql and render the table. Why do you not post the code that you are using. For both pages - the sign-in and then the table page. Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1242503 Share on other sites More sharing options...
axl8910 Posted July 14, 2011 Author Share Posted July 14, 2011 Flexi is just one thing I control through Python script it's not important consider it as just a simple name for something. I need to have both HTML and MYSQL tables because I need to have data inside mysql and I need to have interface on localhost. My login page is not so important because it just redirects to my main page which is currently just partly working. My code is on the beginning of the post=) Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1242581 Share on other sites More sharing options...
sunfighter Posted July 14, 2011 Share Posted July 14, 2011 My code is on the beginning of the post=) I'm sorry but I sent you some things to correct in your php. Please do so and post the new code so we can proceed. And the <body onload needs to be corrected. So I need new, corrected, tested code. Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1242806 Share on other sites More sharing options...
nath2099 Posted July 26, 2011 Share Posted July 26, 2011 Not sure if it's ur issue, but if($q=1) { should be if($q==1) { In the first variation u are telling q to equal 1, and then testing that it does.. it's always going to be true. Quote Link to comment https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/#findComment-1247288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.