axl8910 Posted June 30, 2011 Share Posted June 30, 2011 Hi, I'm new in PHP just doing my first project and I need help. I made login and register script and now I have 2 tables. One is members and one is flexi. What I have to do is to have on PHP page table which assigns flexi1,flexi2,.... to user which is curently logged in. When user presses button flexi table in mysql updates and HTML table changes cell. What I did by now is that when I press button HTML changes OK, but mysql table updates all flexi and user fields ass soon as I refresh page and I need it to update when I click on it, and just one not all. Here is the code: <?php session_start(); require_once("connect.php"); //$query = "SELECT f.flexi, f.status, f.allocation, m.username //FROM flexi f, members m"; $query = "SELECT * FROM flexi"; $result = mysql_query($query); $query1 = 'UPDATE flexi SET allocation = "allocated", user = "'.$_SESSION["username"].'" WHERE flexi="flexi1"'; $result1 = mysql_query($query1); $result1 = $_SESSION["allocate"]; $query2 = 'UPDATE flexi SET allocation = "allocated", user = "'.$_SESSION["username"].'" WHERE flexi="flexi2"'; $result2 = mysql_query($query2); $result2 = $_SESSION["alloc"]; echo "<html> <body> <table border=1 id='mytable'> <tr><th>Flexi</th><th>Status</th><th>Allocation</th><th>User</th></tr>"; if (isset($_SESSION['username'])) { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo' <head> <script type="text/javascript"> function change() { var first="'.$_SESSION['username'].'"; var x=document.getElementById("mytable").rows var y=x[1].cells y[3].innerHTML=first; var all="'.$_SESSION['allocate'].'"; y[2].innerHTML="allocated"; } function cchange() { var second="'.$_SESSION['username'].'"; var x=document.getElementById("mytable").rows var y=x[2].cells y[3].innerHTML=second; var all2="'.$_SESSION['alloc'].'"; y[2].innerHTML="allocated"; } </script> </head> '; 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 "<td><input type='button' style='float:right' onclick='change(this.form)' value='Allocate'></td>"; echo "<td><input type='button' style='float:right' onclick='cchange(this.form)' value='Allocate'></td>"; ?> </table> </body> </html> I know that query is on the start and that is why it updates data as soon as I log in but I don't know how I update it on click. Please help Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/ Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 Anyone? *desparate* Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237177 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 You are gonna need to learn using AJAX for that purpose if you want to update SQL fields after the page load from events like click or onchange. Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237200 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 I implemented ajax now but my buttons don't work anymore xD Here is the code if someone is willing to help: index.html <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } //if(window.event.srcElement == form.button1 xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send(); } function change() { var first="'.$_SESSION['username'].'"; var x=document.getElementById("mytable").rows var y=x[1].cells y[3].innerHTML=first; var all="'.$_SESSION['allocate'].'"; y[2].innerHTML="allocated"; } function cchange() { var second="'.$_SESSION['username'].'"; var x=document.getElementById("mytable").rows var y=x[2].cells y[3].innerHTML=second; var all2="'.$_SESSION['alloc'].'"; y[2].innerHTML="allocated"; } </script> </head> <body onload='showUser(this.value)'> <form> <input type='button' name='button1' onclick='change(this.form)' value='Allocate'> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html> getuser.php <?php $q=$_GET["q"]; session_start(); require_once("connect.php"); $sql="SELECT * FROM flexi"; $result = mysql_query($sql); echo "<table border='1'> <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); ?> How do I transfer username like id so I use instead of this sessions something else which actually works? function change() { var first="'.$_SESSION['username'].'"; var x=document.getElementById("mytable").rows var y=x[1].cells y[3].innerHTML=first; var all="'.$_SESSION['allocate'].'"; y[2].innerHTML="allocated"; Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237205 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 You can't use PHP variables like that inside JavaScript, PHP is parsed on the server BEFORE sending the page to the browser, and javascript on the other hand is parsed in client side. var first="'.$_SESSION['username'].'"; // WRONG This may work... var first="<?php echo $_SESSION['username']; ?>"; Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237207 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 Yeah I just realized it=D this doesn't work either it just leaves empty cell. I need to pass variable from PHP in HTML but I'm not yet sure how to do it. Thx for help Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237210 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 I just showed you in the above post how you can pass a value from php to javascript. What you mean it leaves empty cell? Check in php what does this variable contain with var_dump(); if it has a value in it, it will assign it to the JS variable also. 100% fact. // before echoing it in the js variable check the content, to see if it is what it is supposed to be. var_dump($_SESSION['username']); Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237211 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 when I insert it user changes from "none" to just empty cell in table and it's supposed to be "alale" when I am logged in Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237215 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 I am confused . Try var_dumping your variables and echoing out your sql queries in PHP to see if things are right, and where the values will be gone missing. Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237216 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 In my PHP I wrote echo 'Welcome '.$_SESSION["username"].''; and on php page it says Welcome alale. Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237223 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 <?php // Your php code here, and you say that $_SESSION['username'] has a value here. ?> <html> <head><title></title> <script type="text/javascript"> // Now move that value in a JS variable called first // And it MUST have the value also if your structure is even near to something like this var first = "<?php echo $_SESSION['username']; ?>"; alert(first); </script> </head> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237226 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 Yeah this works in getuser.php file but how to send it through xmlhttp to my main page index.html where I dont have php? Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237239 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 You can't send PHP values in HTML file (unless you have configured your webserver to handle HTML files also through php parser). You have the value stored in $_SESSION-variable so just rename your main file to .php and use session_start(); in the beginning of this file and you got the variable available in that page also. You can though get / set data with PHP via AJAX calls if needed on your main.html. And then process returned AJAX data with JS in the main.html. Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237241 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 Yeah that way I can update HTML table but database stays unchanged=( Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237246 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 Echo the query out ebfore you run it and see if it is as it should be. Also are you getting any errors? Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237247 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 No errors at all I just need to add this query: $query1 = 'UPDATE flexi SET allocation = "allocated", user = "'.$_SESSION["username"].'" WHERE flexi="flexi1"'; $result1 = mysql_query($query1); somewhere in code so it changes my database table when I click on the button which already has javascript function on it to change HTML table cell function change() { 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"; } <input type='button' name='button1' onclick='change(this.form)' value='Allocate'> Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237248 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 You have to do the AJAX call inside the change()-function then. Pass the username as a parameter via AJAX to another PHP script that will be called in the AJAX call. And do the database query in this particular PHP file. Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237252 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 Could you write short example how to do it? I know I am pain in the butt but I have no one else to ask Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237255 Share on other sites More sharing options...
TeNDoLLA Posted July 1, 2011 Share Posted July 1, 2011 Here is a simple example how to do it with jQuery. jQuery is a free javascript library that helps and makes your code much shorter and has some nasty functions to use. You can get it from: http://jquery.com/ . PHP file, where we call the ajax and pass the username to the another PHP file that updates the database based on the click on button1. <?php $username = 'test'; // this is your $_SESSION['username'] ?> <html> <head><title></title> <script type="text/JavaScript" src="../js/jquery-ui-1.8.13.custom/js/jquery-1.5.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // On click even on the button with ID of button1 run this $('#button1').click( function() { $.ajax({ url: "ajax.php", // File to the AJAX file that will update the database type: "GET", // Method = GET data: "&username=<?php echo $username; ?>", // Get paramaters, now we send one called username dataType: "text", // Type of the returned data success: function(data) { alert(data); // Alert a message that is echoed in ajax file on success } }); }); }); </script> <body> <input type='button' id="button1" value='update DB'> </body> </head> </html> Then the AJAX (ajax.php) file <?php if (isset($_GET['username'])) { // Get the username from the AJAX call // Validate it and sanitaze it if needed (not included in here). $username = $_GET['username']; // Then update your database based on the username we just got from AJAX call; variable $username // Check if there db update was successful, if so echo message echo 'Database succesfully updated.' } else { // Else echo error echo 'Error updating database.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237274 Share on other sites More sharing options...
axl8910 Posted July 1, 2011 Author Share Posted July 1, 2011 THX I will try it now=) Quote Link to comment https://forums.phpfreaks.com/topic/240792-phpjavascriptmysql-need-help/#findComment-1237275 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.