PDA

View Full Version : Mixed results with javascript.



max_power
04-24-2009, 11:58 AM
Hi, this php site is for an admin to add, delete and update events now for this one, it can only delete but can not perform the update_data1() and save_data1() functions for some wierd reason. Here is the php code:


<?php
include("includes/host_conf.php");
include("includes/mysql.lib.php");
$obj=new connect;
$mode=$_GET["mode"];

echo "<table width='550' border='0' cellpadding='4' cellspacing='1' bgcolor='#eeeeee' align='center'>";
echo "<tr>";
echo "<td colspan=3> <a href=\"javascript:requestInfo1('showTable.php?mode=new&id=$id','showTable','')\">Add New Events</a>||<a href=\"javascript:requestInfo1('showTable.php?mode=list','showTable','')\">Refresh</a></td>";
echo "</tr>";

echo "<tr>";
echo "<th> ID ";
echo "<th> Author";
echo "<th> Last Updated";
echo "<th> Category";
echo "<th> Event";
echo "<th> Summary";
echo "</tr>";
// For Delete
if($mode=="delete") {
$var_id=$_GET["id"];
$sqlDelete="Delete from events where id='$var_id'";
$obj->query($sqlDelete);
echo "<td><a href=\"javascript:delete_data();\">Delete</a>";
echo "<tr>";
echo "<td colspan=3>Data Deleted";
echo "</tr>";

}
// End of Delete
// For Add New Data -- Display Text box and the Cancel and Save Option
if($mode=="new") {
echo "<tr>";
echo "<td> <input id='id' size=5> ";
echo "<td> <input id='author' size=10> ";
echo "<td> <input id='last_updated' size=10> ";
echo '<td> <select name="category">
<option value="category1">category1</option>
<option value="category2">category2</option>
<option value="category3">category3</option>
<option value="category4">category4</option>
</select> ';
echo '<td> <textarea name="event" rows="12" cols="25"></textarea><td><textarea name="summary" rows="5" cols="25"></textarea> ';
echo "<td<a href=\"javascript:requestInfo1('showTable.php?mode=list&id=$id','showTable','')\">Cancel</a></td>";
echo "<td><a href=\"javascript:save_data1();\">Save</a>";

echo "</tr>";
}
// End of Add New Data
// After Click on Add >> Save option the data is save into the database
if($mode=="save_new") {
$id=$_GET["id"];
$author=$_GET["author"];
$last_updated=$_GET["last_updated"];
$category=$_GET["category"];
$event=$_GET["event"];
$summary=$_GET["summary"];
$sqlSave="Insert into events values('$id','$author','$last_updated','$category','$events','$summary')";
$obj->query($sqlSave);
echo "<tr>";
echo "<td colspan=3>Data Saved";
echo "</tr>";
}
// End of save_new
// For Update save Option
if($mode=="update_data") {
$id=$_GET["id"];
$author=$_GET["author"];
$last_updated=$_GET["last_updated"];
$category=$_GET["category"];
$event=$_GET["event"];
$summary=$_GET["summary"];
$sqlUpdate="Update events set id='$id', author = '$author', last_updated = '$last_updated', category = '$category', event = '$event', summary = '$summary' where id='$prev_id'";
$obj->query($sqlUpdate);
echo "<tr>";
echo "<td colspan=3>Data Updated";
echo "</tr>";
}
// End of Update
// Display all the data from the table
$sql="SELECT id,author,last_updated,category,event,summary FROM events order by id desc";
$obj->query($sql);
while($row=$obj->query_fetch(0)) {
$id=$row['id'];
$author=$row['author'];
$last_updated=$row['last_updated'];
$category=$row['category'];
$event=$row['event'];
$summary=$row['summary'];
echo "<tr bgcolor='#ffffff'>";
// if Mode is Update then get the ID and display the text field with value Other wise print the data into the table
if($mode=="update") {
$id_=$_GET["id"];
}
if($id_==$id) {
echo "<input type='hidden' value='$id_' name='prev_id' id='prev_id'>";
echo "<td> <input type='number' value='$id' id='id' size='5'> </td> ";
echo "<td> <input type='text' value='$author' id='author' size='10'> </td> ";
echo "<td> <input type='text' value='$last_updated' id='last_updated' size='10'> </td> ";
?><td> <select name="category">
<option><? echo $category; ?></option><? echo '
<option value="category1">category1</option>
<option value="category2">category2</option>
<option value="category3">category3</option>
<option value="category4">category4</option>
</select> ';
?><td> <textarea name="event" rows="12" cols="25"><? echo $event; ?></textarea><td><textarea name="summary" rows="5" cols="25"><? echo $summary; ?></textarea> <?
echo "<td> <a href=\"javascript:requestInfo1('showTable.php?mode=list&id=$id','showTable','')\">Cancel</a> </td> ";
echo "<td><a href=\"javascript:update_data1();\">Save</a>";
} else {
echo "<td>$id</td>";
echo "<td>$author</td>";
echo "<td>$last_updated</td>";
echo "<td>$category</td>";
echo "<td>$event</td>";
echo "<td>$summary</td>";
echo "<td> <a href=\"javascript:requestInfo1('showTable.php?mode=update&id=$id','showTable','')\">Update</a> </td> ";
echo "<td> <a href=\"javascript:requestInfo1('showTable.php?mode=delete&id=$id','showTable','');\" onclick='return confirmLink(this);'>Delete</a></td>";
}
echo "</tr>";
}
echo "</table>";
?>


I'll post the javascript code next in a separate post as the characters go over the limit.

max_power
04-24-2009, 11:58 AM
And here is the javascript code:


function getHTTPObject() {
var xmlhttp;

if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

function requestInfo1(url,id,redirectPage) {
var temp=new Array();
http.open("GET", url, true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
if(redirectPage=="" || results!="1") {

var temp=id.split("~"); // To display on multiple div
//alert(temp.length);
var r=results.split("~"); // To display multiple data into the div
//alert(temp.length);
if(temp.length>1) {
for(i=0;i<temp.length;i++) {
//alert(temp[i]);
document.getElementById(temp[i]).innerHTML=r[i];
}
} else {
document.getElementById(id).innerHTML = results;
}
} else {
//alert(results);
window.location.href=redirectPage;
}
}
}
};
http.send(null);
}
function emptyValidation1(fieldList1) {

var field1=new Array();
field1=fieldList1.split("~");
var counter1=0;
for(i=0;i<field1.length;i++) {
if(document.getElementById(field1[i]).value=="") {
document.getElementById(field1[i]).style.backgroundColor="#FF0000";
counter1++;
} else {
document.getElementById(field1[i]).style.backgroundColor="#FFFFFF";
}
}
if(counter1>0) {
alert("The Field mark as red could not left empty");
return false;

} else {
return true;
}

}
function loginProcess() {
var var_username=document.getElementById("username").value;
var var_password=document.getElementById("password").value;

var checkValidation1=emptyValidation1('username~password');

if(checkValidation1==true) {
requestInfo1('login_check.php?username='+var_username+'&password='+var_password,'loginDetails','welcome.php');
} else {
return false;
}
}
function init_table() {
requestInfo1('showTable.php?mode=list','showTable','');
}

function save_data1() {
var id=document.getElementById("id").value;
var author=document.getElementById("author").value;
var last_updated=document.getElementById("last_updated").value;
var category=document.getElementById("category").value;
var event=document.getElementById("event").value;
var summary=document.getElementById("summary").value;
var checkValidation1=emptyValidation1('id~author~last_updated~category~event~summary');

if(checkValidation1==true) {
requestInfo1('showTable.php?mode=save_new&id='+id+'&author='+author+'&last_updated='+last_updated+'&category='+category+'&event='+event+'&summary='+summary,'showTable','');
}
}

function update_data1() {
var prev_id=document.getElementById("prev_id").value;
var id=document.getElementById("id").value;
var author=document.getElementById("author").value;
var last_updated=document.getElementById("last_updated").value;
var category=document.getElementById("category").value;
var event=document.getElementById("event").value;
var summary=document.getElementById("summary").value;
var checkValidation1=emptyValidation1('id~author~last_updated~category~event~summary');

if(checkValidation1==true) {
requestInfo1('showTable.php?mode=update_data&id='+id+'&author='+author+'&last_updated='+last_updated+'&category='+category+'&event='+event+'&summary='+summary+'&prev_id='+prev_id,'showTable','');

}
}

function confirmLink(theLink)
{

var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
if (is_confirmed) {
theLink.href += '';
}

return is_confirmed;
}

Now I dont see any difference to why the javascript shouldnt work? Is there something missing?

Thanks