I am looking for a button that inside a php table.
I tried

echo "<td bgcolor='#FFFFFF'><center>" . '<input type="button" name="Activate" value="Activate" class="form" onClick="activate_user_id('.$rows['username'].','.$rows['loginid'].','.$rows['activated'].')">' . "</center></td>";

I know people said i have to use AJAX to make it work. but i dont know AJAX at all, Can anyone show me how to use AJAX that to make this button works with calling a function in the same page and refresh the page after updated the sql

Recommended Answers

All 3 Replies

tizag has an excellent write up on ajax.
http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php

that said I made you a little function ajax, it currently uses 3 pages.
input form page:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(username,loginid,activated){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var u = username;
	var li = loginid;
	alert('u='+ u);
	alert('li='+li);
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	//var age = document.getElementById('age').value;
	//var wpm = document.getElementById('wpm').value;
	//var sex = document.getElementById('sex').value;
	
	var queryString = "?username=" + username + "&loginid=" + loginid + "&activated=" + activated;
	alert('querystring =' + queryString);
	ajaxRequest.open("GET", "example.php" + queryString, true);
	this.getData(2);
	ajaxRequest.send(null); 	
}
//-->

function getData(number){
	var ajaxRequest;  // The variable that makes Ajax possible!
	//var u = username;
	//var li = loginid;
	//alert('u='+ u);
	//alert('li='+li);
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('rowDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	//var age = document.getElementById('age').value;
	//var wpm = document.getElementById('wpm').value;
	//var sex = document.getElementById('sex').value;
	// in reality, there is no querystring, just call a 'GET' on the page below....
	var queryString = "?rows=" + number;
	
	alert('querystring =' + queryString);
	ajaxRequest.open("GET", "getData.php" + queryString, true);
	ajaxRequest.send(null); 	
}
//-->
</script>
<!--  call you function to get the data you need for inactive users on body onload -->
<body onload="getData(1)";>
<?php


echo "<div id='ajaxDiv'>Your result will display here</div>";  /*  ajaxFunction(x,y,z) will populate this div */
echo"<form><br>";
echo "<div id='rowDiv'></div>";      /* getData() will populate this div */
//foreach($rowses as $rows) {
	//echo "<td bgcolor='#FFFFFF'><center>" . " name:".$rows['username'].' -loginid:'.$rows['loginid'].' -activated:'.$rows['activated'] .'<input type="button" name="Activate" value="Activate" class="form" onClick="ajaxFunction(\''.$rows['username'].'\','.$rows['loginid'].',\'YES\')">' . "</center></td>";
//}
echo"</form>";

example.php

<?php
$dbhost = "localhost";
$dbuser = "dbusername";
$dbpass = "dbpassword";
$dbname = "dbname";
	//Connect to MySQL Server
//mysql_connect($dbhost, $dbuser, $dbpass);
	//Select Database
//mysql_select_db($dbname) or die(mysql_error());
	// Retrieve data from Query String
$username = $_GET['username'];
$loginid = $_GET['loginid'];
$activated = $_GET['activated'];
	// Escape User Input to help prevent SQL Injection
$username = mysql_real_escape_string($username);
$loginid = mysql_real_escape_string($loginid);
$activated = mysql_real_escape_string($activated);
	//build query
	
$query = "update example set active = ".$activated . " WHERE username = " . $username . " and loginid = " . $loginid;
//echo "query 1 = " . $query . "<BR>";
//$qry_result_x = mysql_query($query) or die(mysql_error());


// dont actually run a query.
//$query2 = "select * from example where username = " . $username . " and loginid = " . $loginid;
//$qry_result = mysql_query($query2);
	//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Login Id</th>";
$display_string .= "<th>Activated</th>";

$display_string .= "</tr>";

	// Insert a new row in the table for each person returned
	/*
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<tr>";
	$display_string .= "<td>$row[username]</td>";
	$display_string .= "<td>$row[loginid]</td>";
	$display_string .= "<td>$row[activated]</td>";
	$display_string .= "</tr>";
	
}*/

	$display_string .= "<tr>";
	$display_string .= "<td>$username</td>";
	$display_string .= "<td>$loginid</td>";
	$display_string .= "<td>$activated</td>";
	$display_string .= "</tr>";
	$display_string .= "<tr><td>Has been Activated</td></tr>";


	
// this is also considered responseText.	
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
// Whatever you echo here is the responseText.
echo $display_string;
?>

getData.php

<?php
	// this is just some array data, in theory, you would just connect to a db 
	// with the same query every time.
	
	// select * from table where activated = 'NO';
	// so delete the code below the second /*******/ and do a straight db query like this
/****************************************/
/*  this whole page can consist of the next 15 lines of code or so, it is all you need.
$dbhost = "localhost";
$dbuser = "dbusername";
$dbpass = "dbpassword";
$dbname = "dbname";
	//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
	//Select Database
mysql_select_db($dbname) or die(mysql_error());
	//build query
$query = "select * from table where activated = 'NO'";

$qry_result = mysql_query($query);
	//Build Result String
while($rows = mysql_fetch_array($qry_result)){
	echo "<td bgcolor='#FFFFFF'><center>" . " name:".$rows['username'].' -loginid:'.$rows['loginid'].' -activated:'.$rows['activated'] .'<input type="button" name="Activate" value="Activate" class="form" onClick="ajaxFunction(\''.$rows['username'].'\','.$rows['loginid'].',\'YES\')">' . "</center></td>";
}
    you can delete everything after this.
/****************************************/
	$rows=$_GET["rows"];
	$rowses=array();	
	if ($rows == 1) {
		$row['username'] = 'Dana';
		$row['loginid'] = 1;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Bill';
		$row['loginid'] = 2;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Ted';
		$row['loginid'] = 3;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Alice';
		$row['loginid'] = 4;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Mila';
		$row['loginid'] = 5;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Dan';
		$row['loginid'] = 6;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Joel';
		$row['loginid'] = 7;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Erik';
		$row['loginid'] = 8;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Tara';
		$row['loginid'] = 9;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
	} else if ($rows == 2) {
		$row['username'] = 'Bill';
		$row['loginid'] = 2;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Ted';
		$row['loginid'] = 3;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Alice';
		$row['loginid'] = 4;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Mila';
		$row['loginid'] = 5;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Dan';
		$row['loginid'] = 6;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Joel';
		$row['loginid'] = 7;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Erik';
		$row['loginid'] = 8;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
		$row['username'] = 'Tara';
		$row['loginid'] = 9;
		$row['activated'] = 'NO';
		array_push($rowses,$row);
    }		
//	return $rowses;
// then based on the results of that query, display your remaining people that are not active on this page.
foreach($rowses as $rows) {
	echo "<td bgcolor='#FFFFFF'><center>" . " name:".$rows['username'].' -loginid:'.$rows['loginid'].' -activated:'.$rows['activated'] .'<input type="button" name="Activate" value="Activate" class="form" onClick="ajaxFunction(\''.$rows['username'].'\','.$rows['loginid'].',\'YES\')">' . "</center></td>";
}

in my example code I initially getData loads the first array and all other times loads the second one, missing the first record. you will not need to put a constraint on getData as you want the 'same' data everytime, everybody who is not approved is what I am guessing.

Enjoy.

Thank you so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.