簡體   English   中英

將mysql_query添加到onclick函數中

[英]add a mysql_query into an onclick function

我要這樣做,以便只有在前一個框中的信息正確的情況下才出現新的輸入框,通過使用mysql_query進行檢查,我可以調整此代碼

function show(id){
  if(document.getElementById(id+"-link").style.display=="none") {
     document.getElementById(id+"-link").style.display="block";
     document.getElementById(id+"-input").style.display="block";
  }
}

<tr> 
  <td><input type="text" id="a1" name="em_1"></td>
  <td><a href="#null" onclick="show('b1')">and</a></td>
</tr><tr>
  <td><input type="text" id="b1-input" name="em_2" style="display:none"></td>
  <td><a href="#null" style="display:none" id="b1-link"</a></td>
</tr><tr>

所以我需要在這里添加:

$userCheck=mysql_query(SELECT email FROM users WHERE name = "em_1")
$row=mysql_num_rows($usercheck)
if($row!==0){"show('b1')";}

或其他影響因素,而無需刷新頁面。 這可能嗎?

是的,有可能,但是您需要知道如何使用ajaxjquery-ajax

這將幫助您將數據從php獲取到javascript ,然后附加到html

阿賈克斯:

function XMLDoc()
{
if (window.XMLHttpRequest)
  {
    xmlhttp=new XMLHttpRequest();
  }
else
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            // xmlhttp.responseText -> return value from php file , so from here you can do your action
        }
    };
xmlhttp.open("POST","yourfile.php?email={value}",true); // from here send value to check in server side
xmlhttp.send(); 
}

Php:

if (isset($_POST['email'])){ // which is variable from `yourfile.php?email`
    $userCheck=mysql_query("SELECT email FROM users WHERE name = 'em_1'");
    $row=mysql_num_rows($usercheck);
    if ($row==0){
        echo "no";
    } else {
        echo "yes";
    }
}

傳遞服務器端腳本的yourfile URL,該腳本檢查電子郵件是否存在並返回,例如返回yes或no =>然后在Ajax的幫助下它將返回當前文檔

注意 :請勿使用mysql擴展名,而應使用mysqliPDO

您需要AJAX才能使這項工作無需刷新。 在W3Schools上查看此頁面: http : //www.w3schools.com/php/php_ajax_database.asp

幾乎完全是您想要的,但是要使b1片段充滿活力,而不是返回文本塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM