检查用户名可用性问题

时间:2012-01-12 18:01:08

标签: php javascript ajax joomla1.5

我只是在我的Joomla TPJOBS组件中添加了一个新功能,虽然它不是一个好的/完整/活动的组件,但只是我添加了一个用户名可用性检查器,但它不起作用,因为它可能是由于在这个组件中。

我的Joomla组件目录:\ public_html \ components \ com_tpjobs \

编辑 tpjobs.html.php(退出)和username_validate.php(已创建)

我的Javascript AJAX代码:在tpjobs.html.php下

// JavaScript 
<script type="text/javascript">
<!--
//function to create ajax object
function pullAjax(){
    var a;
    try{
        a = new XMLHttpRequest()
    }
    catch(b)
    {
        try
        {
            a = new ActiveXObject("Msxml2.XMLHTTP")
        }
        catch(b)
        {
            try
            {
                a = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch(b)
            {
                alert("Your browser broke!"); return false
            }
        }
    }
    return a;
}

function <?php
defined('_JEXEC') or die('Restricted access');


$user = strip_tags(trim($_REQUEST['username']));

if(strlen($user) <= 0)
{
  echo json_encode(array('code'  =>  -1,
  'result'  =>  'Invalid username, please try again.'
  ));
  die;
}


$query = "Select a.username FROM #__users where username = '$user' ";


$db =& JFactory::getDBO();

$result = $db->setQuery($query);

$available = mysql_num_rows($result);

if($available)
{
  echo json_encode(array('code'  =>  1,
  'result'  =>  "Success,username $user is still available"
  ));
  die;
}
else
{
  echo  json_encode(array('code'  =>  0,
  'result'  =>  "Sorry but username $user is already taken."
  ));
  die;
}
die;
?>()
{
    site_root = '';
    var x = document.getElementById('username');
    var msg = document.getElementById('msg');
    user = x.value;

    code = '';
    message = '';
    obj = pullAjax();
    obj.onreadystatechange = function(){
        if(obj.readyState == 4)
        {
            eval("result = " + obj.responseText);
            code = result['code'];
            message = result['result'];
            if(code <=0)
            {
                x.style.border = "1px solid #FF0084";
                msg.style.color = "#FF1A00";
            }
            else
            {
                x.style.border = "1px solid #008C00";
                msg.style.color = "#73880A";
            }
            msg.innerHTML = message;
        }
    }
    obj.open("GET", site_root + "username_validate.php?username=" + user, true);
    obj.send(null);
}
//-->
</script>
<style>
#username{border: 1px solid #000;}
</style>

档案 username_validate.php

<?php
defined('_JEXEC') or die('Restricted access');

$user = strip_tags(trim($_REQUEST['username']));

if(strlen($user) <= 0)
{
  echo json_encode(array('code'  =>  -1,
  'result'  =>  'Invalid username, please try again.'
  ));
  die;
}

// Query database to check if the username is available
$query = "Select a.username FROM #__users where username = '$user' ";

$available = true;

if($available)
{
  echo json_encode(array('code'  =>  1,
  'result'  =>  "Success,username $user is still available"
  ));
  die;
}
else
{
  echo  json_encode(array('code'  =>  0,
  'result'  =>  "Sorry but username $user is already taken."
  ));
  die;
}
die;
?>

组件表单:

<form action="index.php" method="post" name="regJobSeekerNew" enctype="multipart/form-data">
  <div class="col width-60">
    <fieldset class="tpj_fieldset">
      <legend><?php echo JText::_('USER INFORMATION'); ?></legend>
      <table class="admintable">
        <tr>
          <td class="key"><label for="first_name"> <?php echo JText::_('FIRST NAME'); ?>: </label></td>
          <td><input type="text" size="50" maxlength="100" name="first_name" id="first_name" class="inputbox"></td>
        </tr>
        <tr>
          <td class="key"><label for="last_name"> <?php echo JText::_('LAST NAME'); ?>: </label></td>
          <td><input type="text" size="50" maxlength="100" name="last_name" id="last_name" class="inputbox"></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('USERNAME'); ?>: </label></td>
          <td><input type="text" size="50" maxlength="100" name="username" id="username" class="inputbox"><input type="button" onclick="username_validate();" value="Check Availability"/><br /><div id="msg"></div></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('EMAIL'); ?>: </label></td>
          <td><input type="text" size="50" maxlength="100" name="email" id="email" class="inputbox"></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('PASSWORD'); ?>: </label></td>
          <td><input type="password" size="50" maxlength="100" name="password" id="password" class="inputbox"></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('VERIFY PASSWORD'); ?>: </label></td>
          <td><input type="password" size="50" maxlength="100" name="password2" id="password2" class="inputbox"></td>
        </tr>
      </table>
    </fieldset>
  </div>
  <div class="col width-60">
    <fieldset class="tpj_fieldset">
      <legend><?php echo JText::_('EXPERIENCE / EDUCATION'); ?></legend>
      <table class="admintable">
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('CURRENT POSITION'); ?>: </label></td>
          <td><input class="inputbox" type="text" name="current_position" id="current_position" size="60" maxlength="100" value="" /></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('MAJOR'); ?>: </label></td>
          <td><?php 
                        $list_major = getSelectMajor('id_major', '', '');
                        echo $list_major;
                        ?></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('DEGREE LEVEL'); ?>: </label></td>
          <td><?php 
                        $list_degree_level = getSelectDegreeLevel('id_degree_level', '', '');                                               echo $list_degree_level;
                    ?></td>
        </tr>
      </table>
    </fieldset>
  </div>
  <div class="col width-60">
    <fieldset class="tpj_fieldset">
      <legend><?php echo JText::_('DESIRED EMPLOYMENT'); ?></legend>
      <table class="admintable">
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('PRIMARY INDUSTRY'); ?>: </label></td>
          <td><?php 
                        $list_primary_industry = getSelectIndustry('id_industry1', '', '');
                        echo $list_primary_industry;                    
                        ?></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('SECONDARY INDUSTRY'); ?>: </label></td>
          <td><?php 
                        $list_secondary_industry = getSelectIndustry('id_industry2', '', '');
                        echo $list_secondary_industry;                  
                        ?></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('POSITION TYPE'); ?>: </label></td>
          <td><?php 
                        $list_position_type = getSelectPositionType('id_pos_type', '', '');
                        echo $list_position_type;                   
                        ?></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('MINIMUM SALARY'); ?>: </label></td>
          <td nowrap="nowrap"><input class="inputbox" type="text" name="min_salary" id="min_salary" size="40" maxlength="100" value="" />
            <?php 
                        $list_salary_type = getSelectTypeSalary('id_type_salary', '', '');
                        echo $list_salary_type;                 
                        ?></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('IN CURRENCY'); ?>: </label></td>
          <td nowrap="nowrap"><input class="inputbox" type="text" name="currency_salary" id="currency_salary" size="10" maxlength="100" value="" /></td>
        </tr>
        <tr>
          <td class="key"><label for="name"> <?php echo JText::_('UPLOAD A PHOTO'); ?>: </label></td>
          <td nowrap="nowrap"><input type="file" name="photo" class="inputbox" /></td>
        </tr>
      </table>
    </fieldset>
  </div>
  <?php
            $rowid = (!empty($row->user_id)) ? $row->user_id : null;
            HTML_front_tpjobs::showCustom($custom, $rowid);
            ?>
  <?php
            $config = & JComponentHelper::getParams('com_tpjobs');  
            $termid = $config->get('termarticleid');
            $link = JRoute::_("index.php?option=com_content&view=article&id=".$termid);
            ?>
  <p>
    <?php
global $mainframe;
$mainframe->triggerEvent('onShowOSOLCaptcha', array(false));
?>
  </p>
  <p><?php echo JText::sprintf('BY CLICKING YOU ARE AGREE', $link); ?></a></p>
  <input type="button" value="<?php echo JText::_( 'I ACCEPT CREATE MY ACCOUNT' ); ?>" class="button" onClick="validateregJobSeekerNew()" />
  <input type="hidden" name="option" value="<?php echo $option; ?>" />
  <input type="hidden" name="task" value="savejobseekernew" />
  <input type="hidden" name="id" value="" />
  <input type="hidden" name="boxchecked" value="0" />
  <?php echo JHTML::_('form.token'); ?>
</form>
<?php
    }

1 个答案:

答案 0 :(得分:2)

<?php
defined('_JEXEC') or die('Restricted access');


$user = strip_tags(trim($_REQUEST['username']));

if(strlen($user) <= 0)
{
  echo json_encode(array('code'  =>  -1,
  'result'  =>  'Invalid username, please try again.'
  ));
  die;
}

// Query database to check if the username is available
$query = "Select a.username FROM #__users where username = '$user' ";
// Execute the above query using your own script and if it return you the
// result (row) we should return negative, else a success message.

$db =& JFactory::getDBO();

$result = $db->setQuery($query);

$available = mysql_num_rows($result);

if($available)
{
  echo json_encode(array('code'  =>  1,
  'result'  =>  "Success,username $user is still available"
  ));
  die;
}
else
{
  echo  json_encode(array('code'  =>  0,
  'result'  =>  "Sorry but username $user is already taken."
  ));
  die;
}
die;
?>