Jump to content

Archived

This topic is now archived and is closed to further replies.

girlzz

[SOLVED] automatic display

Recommended Posts

i had 2 textfield which contain id and name... my problem is.. when i insert id, i want textfield name automatic detect the id that i had inserted...

 

id :______

(this id will be key in by user)

name:________

(when user key in id, then automatic appear the name)...

 

i found lot of ajax script but i'm not familiar with ajax eventhough i had read it several time, so i prefer if somebody can help me solved this problem.. and how can i retrieve data from database using ajax and php.. thanks

Share this post


Link to post
Share on other sites

are all the id's the same length? or do you want it to look for a name after every key press?

 

what is this being used for?  as if you have it check after every key press, people can "fish" for name/id associations by just typing in numbers to see who belongs to what id.  that could be a security risk.

 

answer these questions and i should be able to better help you. 

 

later.

 

-grant

Share this post


Link to post
Share on other sites

hurm my id actually is a matric number that used to identified which student hold this number. those matric number are in the same length... so when i insert the matric number, then without click any button and without refreshing the page the name is automatic display in the textfield...

Share this post


Link to post
Share on other sites

the first thing you want ot get working is just the javascript side of things.  don't worry about the ajax and php at first.

 

create a function that is called everytime a key is pressed  (onkeyup = "check();")

 

in your javascript check the length of what is being entered and also check to see if it's a number.  the way i do that is divide it by 1 and see if it's equal to the original input.  if it's a number this should be true.

 

i'd say get that working first.  make it so that when you type the right length of numbers into the text box, another text box is updated to just say that you reached the right amount of numbers. 

hint: also use maxlength in the input to limit the max number of characters that can be entered.

 

let me know when you get that workin.

Share this post


Link to post
Share on other sites

ok i nearly understand about ajax because i had try to make drop down menu and display it in the form it's work but why it keep return a problem say code can't be null??? for your information, i make drop down menu called year.. when select i of the item, it will display info about the year such as code... but i try to save it but unsucessful..

Share this post


Link to post
Share on other sites

so did you get the javascript working where it checks the length of what's being typed in and whether or not its a number....and then displays whether or not the input meets those two requirements?

Share this post


Link to post
Share on other sites

i dont get it done yet but i tried to do something else like the drop down using ajax it's work! but i can't figure out why it keep said s_code cannot be null when i tried to save the form... how i want the form can be save into the database??

Share this post


Link to post
Share on other sites
<?php require_once('Connections/mas.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "login.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["name"])) && ($_POST["name"] != "")) {
  $insertSQL = sprintf("INSERT INTO medic_block_reg (matric_no, name, sessi, `year`, `group`) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['matric'], "text"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['sessi'], "text"),
                       GetSQLValueString($_POST['year'], "text"),
                       GetSQLValueString($_POST['group'], "int"));
				   
$insertSQL2 = sprintf("INSERT INTO medic_registered_course (matric_no, name, sessi, `year`,`s_code`, `group`) VALUES ( %s, %s, %s, %s,%s, %s)",
                       GetSQLValueString($_POST['matric'], "text"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['sessi'], "text"),
                       GetSQLValueString($_POST['year'], "text"),
			        GetSQLValueString($_GET['value'], "text"),
                       GetSQLValueString($_POST['group'], "int"));
				   
				   
  mysql_select_db($database_mas, $mas);
  $Result1 = mysql_query($insertSQL, $mas) or die(mysql_error());
  $Result2 = mysql_query($insertSQL2, $mas) or die(mysql_error());
  $saved = true;
}


$colname_rs_mas = "-1";
if (isset($_POST['matric'])) {
  $colname_rs_mas = $_POST['matric'];
}
mysql_select_db($database_mas, $mas);
$query_rs_mas = sprintf("SELECT matric_no, name FROM medic_student WHERE matric_no = %s", GetSQLValueString($colname_rs_mas, "text"));
$rs_mas = mysql_query($query_rs_mas, $mas) or die(mysql_error());
$row_rs_mas = mysql_fetch_assoc($rs_mas);
$totalRows_rs_mas = mysql_num_rows($rs_mas);
?>
<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("mas", $con);

$sql="SELECT `s_code` FROM `medic_subject_offered` WHERE `year` = '".$q."'";

$result = mysql_query($sql);
$s_code = $_POST['q'];


$q_sql = "INSERT INTO medic_registered_course (s_code) VALUES('$q')";


   if( @mysql_query ($q_sql) ){ 
           echo 'You\'re shift has been processed. Thank you. ';
           
       } 
       else {
       echo 'An error has been encountered. Please refresh the screen. ';
       }
    


while($row = mysql_fetch_array($result))
{

echo $row['s_code']  ;

}


mysql_close($con);
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Block Registration | Medic Assessment System</title>
<link href="css/lgblue.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif}
.style10 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; }
.style12 {font-family: Arial, Helvetica, sans-serif; font-size: 14px; }
.style16 {font-size: 18px;
color: #000000;
}
.style8 {font-family: Arial, Helvetica, sans-serif; font-size: 10; }
.style17 {
font-style: italic;
font-size: small;
font-weight: bold;
}
.style18 {
font-size: 14px;
font-weight: bold;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
body,td,th {
color: #996600;
}
.style23 {color: #000000}
.style25 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; }
.style26 {
font-size: 12px;
font-weight: bold;
color: #000000;
}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
function MM_controlSound(x, _sndObj, sndFile) { //v3.0
  var i, method = "", sndObj = eval(_sndObj);
  if (sndObj != null) {
    if (navigator.appName == 'Netscape') method = "Play";
    else {
      if (window.MM_WMP == null) {
        window.MM_WMP = false;
        for(i in sndObj) if (i == "ActiveMovie") {
          window.MM_WMP = true; break;
      } }
      if (window.MM_WMP) method = "play";
      else if (sndObj.FileName) method = "run";
  } }
  if (method) eval(_sndObj+"."+method+"()");
  else window.location = sndFile;
}
//-->
</script>

<script src="selectyear.js">
// JavaScript Documentvar xmlHttp

function showYear(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getyear.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("txtHint2").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;
}
</script>

</head>

<body <?php if($saved){echo 'onload="alert(\'One record successfully saved.\');"';}?>>

<form id="form1" name="form1" method="POST">


    <table width="603" height="352" align="center" bordercolor="#CCFFCC" class="boxads">
      <tr>
        <td height="65" colspan="5" bgcolor="#FFFFCC"><p align="center" class="style16"> </p>
            <p align="center" class="style16 style18">Medic Assessment System </p>
          <p align="center" class="style16"> </p></td>
      </tr>
      <tr>
        <td height="29" colspan="5" bgcolor="#FFFFFF" class="style12"><div align="center" class="style10"><span class="style17 style23">Block Registration</span></div>
<span class="style8">
            <label></label>
          </span><span class="style8">
            <label></label>
            </span><span class="style1"></span><span class="style1">
            <label></label>
            </span>
            <div align="center" class="style17"></div></td>
      </tr>
      <tr>
        <td width="80" height="35" bgcolor="#CCFFCC"><div align="right" class="style26">Matric No</div></td>
        <td width="18" bgcolor="#CCFFCC"><div align="center" class="style26">:</div></td>
<td colspan="3" bgcolor="#CCFFCC"><div align="left"><input name="matric" type="text" class="style26" id="matric" value="<?php echo $row_rs_mas['matric_no']; ?>" size="15" />
  
</div></td>
      </tr>
      <tr>
        <td height="33" bgcolor="#CCFFCC"><div align="right" class="style26">Name</div></td>
        <td width="18" bgcolor="#CCFFCC"><div align="center" class="style26">:</div></td>
        <td colspan="3" bgcolor="#CCFFCC"><div align="left">
          <p>
            </span>
            <input name="name" type="text" class="style26" id="name" value="<?php echo $row_rs_mas['name']; ?>" size="60" />
          </p>
        </div></td>
      </tr>
      <tr>
        <td height="117" bgcolor="#CCFFCC"><div align="right"><span class="style26">Sessi</span></div></td>
        <td bgcolor="#CCFFCC"><div align="center" class="style26"><strong>:</strong></div></td>
        <td bgcolor="#CCFFCC"><div align="left">
          <input name="sessi" type="text" class="style26" id="sessi" size="15" maxlength="9" />
        </div></td>
        <td bgcolor="#CCFFCC"><div align="right"><span class="style26">Year:</span></div></td>
        <td bgcolor="#CCFFCC"><p>
        
          <p>          
          <p><span class="style23">
            <select name="year" class="style26" onchange="showYear(this.value)">
              <option value="-"> - </option>
              <option value="1 block 1"> 1 block 1</option>
              <option value="1 block 2"> 1 block 2</option>
              <option value="1 block 3"> 1 block 3</option>
              <option value="1 block 4"> 1 block 4</option>
              <option value=" - "> - </option>
              <option value="2 block 1"> 2 block 1</option>
              <option value="2 block 2"> 2 block 2</option>
              <option value="2 block 3"> 2 block 3</option>
              <option value="2 block 4"> 2 block 4</option>
              <option value="- "> - </option>
              <option value="3 block 1"> 3 block 1</option>
              <option value="3 block 2"> 3 block 2</option>
              <option value="3 block 3"> 3 block 3</option>
              <option value="3 block 4"> 3 block 4</option>
              <option value=" - "> - </option>
              <option value="4 block 1"> 4 block 1</option>
              <option value="4 block 2"> 4 block 2</option>
              <option value="4 block 3"> 4 block 3</option>
              <option value="4 block 4"> 4 block 4</option>
              <option value="4 block 5"> 4 block 5</option>
              <option value="4 block 6"> 4 block 6</option>
              <option value="4 block 7"> 4 block 7</option>
              <option value=" - "> - </option>
              <option value="5 block 1"> 5 block 1</option>
              <option value="5 block 2"> 5 block 2</option>
              <option value="5 block 3"> 5 block 3</option>
              <option value="5 block 4"> 5 block 4</option>
              <option value="5 block 5"> 5 block 5</option>
                                    </select>
            </span>
            
            </span>
           <br />
           <span class="style26"></span><span class="style26"><div class="style26" id="txtHint" name = "txtHint">
           <span class="style1">subject code info will be listed here.</div>
           </p>
           </span></td>
      </tr> <input type="hidden" name = "txtHint2" value="<?php echo $row_s_code['s_code']; ?>">
            <td height="2"></input>
      
      <tr>
        <td height="27" bgcolor="#CCFFCC"><div align="right" class="style26">Group</div></td>
        <td bgcolor="#CCFFCC"><div align="center" class="style26">:</div></td>
        <td width="90" bgcolor="#CCFFCC"><div align="left">
          <p class="style25">
            <select name="group" size="1" class="style26" id="group">
              <option value="-">-</option>
              <option value="1" >1</option>
              <option value="2" >2</option>
              <option value="3" >3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              <option value="7">7</option>
              <option value="8">8</option>
              <option value="9">9</option>
              <option value="10">10</option>
              <option value="11">11</option>
              <option value="12">12</option>
            </select>
            </p>
        </div></td>
        <td width="71" bgcolor="#CCFFCC"><div align="right"></div></td>
        <td width="320" bgcolor="#CCFFCC"> </td>
      </tr>
      
      <tr>
        <td height="24" colspan="5" bgcolor="#CCFFCC"><div align="center">
          <input name="save" type="submit" class="style26" id="save" value="Save" />
          <input name="Reset" type="reset" class="style26" value="Reset" />
        </div>
            <div align="center"></div></td>
      </tr>
    </table>
<div align="center">
      <p> </p>
      <p class="style10"><a href="reg_main.php">Back</a> | <a href="main.php">Module Menu</a> | <a href="reg_query.php"> View Registered Student</a> | <a href="<?php echo $logoutAction ?>" onclick="MM_controlSound('play','document.CS1210559072500','Windows Notify.wav');MM_popupMsg('You\'re successfully logout from this system...')">Logout</a></p>
    </div>
    <p align="center"> </p>
  </div>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<embed name="CS1210559072500" src="Windows Notify.wav" loop="false" autostart="false" hidden="true" width="0" height="0" enablejavascript="true"></embed>
</body>

<?php
if ((isset($_POST["block"])) && ($_POST["block"] == "")) {
mysql_free_result($rs_mas);

mysql_free_result($scode);

}

?>
</html>

this is my coding..... can you help me sir??

Share this post


Link to post
Share on other sites
<form id="form1" name="form1" method="POST">

 

remove "method = "POST""  this will mess stuff up as your form isn't actually submitting the info, the javascript is.  and in the javascript you are using GET neways.  that might fix stuff.

Share this post


Link to post
Share on other sites

ok like this..... this form is for registration... if admin key in matric then automatic display name but for those i don't get it done yet... then admin just insert session,group n year.. when admin select year, it will auto display s_code..... i already done it by using ajax.. but when i want to save it.. the error keep return to s_code can't be null....

Share this post


Link to post
Share on other sites
$insertSQL2 = sprintf("INSERT INTO medic_registered_course (matric_no, name, sessi, `year`,`s_code`, `group`) VALUES ( %s, %s, %s, %s,%s, %s)",

 

i think what's happening is that in your mysql database, the field s_code is text.  am I right?

 

if so you might not be entering text, but something else.  make sure you have single quotes around the variable that's being entered into s_code

 

i've never used "GetSQLValueString" so i'm not sure if what you're doing wrong is in there.

 

though i'm realy sure it's in your php when you go to upload.  you're trying ot insert something into s_code that doesn't match the format designated in the database for that field.

Share this post


Link to post
Share on other sites

my s_code like this MED 1112... so should i change from text to another else?

Share this post


Link to post
Share on other sites
 GetSQLValueString($_GET['txtHint'], "varchar"),

like this??

Share this post


Link to post
Share on other sites

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1)' at line 1 <------------ what is this mean???

Share this post


Link to post
Share on other sites
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET["name"])) && ($_GET["name"] != "")) {
  $insertSQL = sprintf("INSERT INTO medic_block_reg (matric_no, name, sessi, `year`, `group`) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_GET['matric'], "text"),
                       GetSQLValueString($_GET['name'], "text"),
                       GetSQLValueString($_GET['sessi'], "text"),
                       GetSQLValueString($_GET['year'], "text"),
                       GetSQLValueString($_GET['group'], "int"));
				   
$insertSQL2 = sprintf("INSERT INTO medic_registered_course (matric_no, name, sessi, `year`,`s_code`, `group`) VALUES ( %s, %s, %s, %s,%s, %s)",
                       GetSQLValueString($_GET['matric'], "text"),
                       GetSQLValueString($_GET['name'], "text"),
                       GetSQLValueString($_GET['sessi'], "text"),
                       GetSQLValueString($_GET['year'], "text"),
			        GetSQLValueString($_GET['txtHint'], "varchar"),
                       GetSQLValueString($_GET['group'], "int"));
				   
				   
  mysql_select_db($database_mas, $mas);
  $Result1 = mysql_query($insertSQL, $mas) or die(mysql_error());
  $Result2 = mysql_query($insertSQL2, $mas) or die(mysql_error());
  $saved = true;
}


$colname_rs_mas = "-1";
if (isset($_GET['matric'])) {
  $colname_rs_mas = $_GET['matric'];
}
mysql_select_db($database_mas, $mas);
$query_rs_mas = sprintf("SELECT matric_no, name FROM medic_student WHERE matric_no = %s", GetSQLValueString($colname_rs_mas, "text"));
$rs_mas = mysql_query($query_rs_mas, $mas) or die(mysql_error());
$row_rs_mas = mysql_fetch_assoc($rs_mas);
$totalRows_rs_mas = mysql_num_rows($rs_mas);
?>

Share this post


Link to post
Share on other sites

×
×
  • 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.