Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/241061-fail-in-passing-variables/
Share on other sites

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?

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.

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=)

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.

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=)

  • 2 weeks later...
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.