Jump to content

Passing radio button status once data is fetched


evz0001

Recommended Posts

I have 2 files that does the following:

 

- 1 x form that uses ajax to fetch name & last name once id number has been input. 

- 1 x script that passes the info from database to form above

 

My problem is:

 

If the data is available (caus of valid id number) I want to activate (enable) the edit & delete radio button on the form else if the id number isn't valid I want to enable the add radio button.

 

Thanks

----------------------------------------------------

Code for the form:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<style type="text/css">

body{

background-repeat:no-repeat;

font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;

height:100%;

background-color: #FFF;

margin:0px;

padding:0px;

background-image:url('/images/heading3.gif');

background-repeat:no-repeat;

padding-top:85px;

}

 

fieldset{

width:500px;

margin-left:10px;

}

 

</style>

<script type="text/javascript" src="js/ajax.js"></script>

<script type="text/javascript">

/************************************************************************************************************

Ajax client lookup

Copyright © 2006  DTHMLGoodies.com, Alf Magne Kalleland

 

This library is free software; you can redistribute it and/or

modify it under the terms of the GNU Lesser General Public

License as published by the Free Software Foundation; either

version 2.1 of the License, or (at your option) any later version.

 

This library is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

Lesser General Public License for more details.

 

You should have received a copy of the GNU Lesser General Public

License along with this library; if not, write to the Free Software

Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

 

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script

written by Alf Magne Kalleland.

 

Alf Magne Kalleland, 2006

Owner of DHTMLgoodies.com

 

 

************************************************************************************************************/

var ajax = new sack();

var currentClientID=false;

function getClientData()

{

var clientId = document.getElementById('clientID').value.replace(/[^0-9]/g,'');

if(clientId.length==13 && clientId!=currentClientID){

currentClientID = clientId

ajax.requestFile = 'getClient.php?getClientId='+clientId; // Specifying which file to get

ajax.onCompletion = showClientData; // Specify function that will be executed after file has been found

ajax.runAJAX(); // Execute AJAX function

}

 

}

 

function showClientData()

{

var formObj = document.forms['clientForm'];

eval(ajax.response);

}

 

 

function initFormEvents()

{

document.getElementById('clientID').onblur = getClientData;

document.getElementById('clientID').focus();

}

 

 

window.onload = initFormEvents;

</script>

 

</head>

<body>

<form name="clientForm" action="ajax-client_lookup.html" method="post">

<fieldset>

<legend><b>ID Number</b></legend>

<table>

<tr>

<td><label for="clientID">ID number:</label></td>

<td><input name="clientID" id="clientID" size="13" maxlength="13"></td>

</tr>

<tr>

<td><label for="firstname">First name:</label></td>

<td><input name="firstname" id="firstname" size="20" maxlength="255" disabled="Yes"></td>

</tr>

<tr>

<td><label for="lastname">Last name:</label></td>

<td><input name="lastname" id="lastname" size="20" maxlength="255" disabled="Yes"></td>

</tr>

</table>

    <p>

      <input name="add" type="radio" value="add_contact" disabled="yes">

Add Contact<br>

<input name="edit" type="radio" value="edit_contact" disabled="yes">

Edit Contact<br>

      <input name="delete" type="radio" value="delete_contact" disabled="yes">

Delete Contact</p>

      </fieldset>

</form>

 

</body>

</html>

----------------------------------------------------

 

code for script that fetch data

 

<?php

 

if(isset($_GET['getClientId'])){ 

  $res = mysql_query("select * from contacts where idnumber='".$_GET['getClientId']."'") or die(mysql_error());

  if($inf = mysql_fetch_array($res)){

    echo "formObj.firstname.value = '".$inf["firstname"]."';\n";   

    echo "formObj.lastname.value = '".$inf["lastname"]."';\n";

document.clientForm.edit.disabled = no;

document.clientForm.delete.disabled = no;   

     

  }else{

    document.clientForm.add.disabled = no;   

         

  }   

}

?>

 

 

-----------------------------------------------------

Link to comment
Share on other sites

Hello,

 

Please change your code for script that fetch data with following code.

 

<?php

 

if(isset($_GET['getClientId'])){

  $res = mysql_query("select * from contacts where idnumber='".$_GET['getClientId']."'") or die(mysql_error());

  if($inf = mysql_fetch_array($res)){

    echo "formObj.firstname.value = '".$inf["firstname"]."';\n"; 

    echo "formObj.lastname.value = '".$inf["lastname"]."';\n";

    echo "document.clientForm.edit.disabled = no;";

    echo "document.clientForm.delete.disabled = no;";

     

  }else{

    echo "document.clientForm.add.disabled = no;";

         

  } 

}

?>

 

Hope this will solve your problem;

 

Thanks.

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.