Jump to content

PHP+javascript+mysql need help


axl8910

Recommended Posts

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  :-[

Link to comment
Share on other sites

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";

 

Link to comment
Share on other sites

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']; ?>";	

Link to comment
Share on other sites

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']);

 

 

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.