2013-11-15 98 views
0

我有一個使用一些簡單的PHP來提交表單的Joomla網站,但是我喜歡能夠自動添加換行符,只要在輸入我的文本時按下Enter鍵,以及自動將任何網址轉換爲一個可點擊的鏈接。這是可能的,任何人都可以告訴我怎麼樣?添加自動換行符和超鏈接到textarea字段?

<?php 
# FileName="Connection_php_mysql.htm" 
# Type="MYSQL" 
# HTTP="true" 
$hostname_spectac = "localhost"; 
$database_spectac = "database"; 
$username_spectac = "username"; 
$password_spectac = "password"; 
$spectac = mysql_pconnect($hostname_spectac, $username_spectac, $password_spectac) or trigger_error(mysql_error(),E_USER_ERROR); 
?> 
<?php 
if(isset($_POST['eventDate'])){ 
$date = date('Y-m-d',strtotime($_POST['eventDate'])); 
} 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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["MM_update"])) && ($_POST["MM_update"] == "form1")) { 
    $updateSQL = sprintf("UPDATE tblEventCustom SET eventDate=%s, eventFile=%s, eventActive=%s, eventCost=%s, eventName=%s, eventFriendlyName=%s, eventDesc=%s WHERE eventId=%s", 
         GetSQLValueString($date, "date"), 
         GetSQLValueString($_POST['eventFile'], "text"), 
         GetSQLValueString($_POST['eventActive'], "int"), 
         GetSQLValueString($_POST['eventCost'], "int"), 
         GetSQLValueString($_POST['eventName'], "text"), 
         GetSQLValueString($_POST['eventFriendlyName'], "text"), 
         GetSQLValueString($_POST['eventDesc'], "text"), 
         GetSQLValueString($_POST['eventId'], "int")); 

    mysql_select_db($database_spectac, $spectac); 
    $Result1 = mysql_query($updateSQL, $spectac) or die(mysql_error()); 
} 

$colname_getEventDetails = "-1"; 
if (isset($_POST['eventId'])) { 
    $colname_getEventDetails = $_POST['eventId']; 
} 
mysql_select_db($database_spectac, $spectac); 
$query_getEventDetails = sprintf("SELECT * FROM tblEventCustom WHERE eventId = %s", GetSQLValueString($colname_getEventDetails, "int")); 
$getEventDetails = mysql_query($query_getEventDetails, $spectac) or die(mysql_error()); 
$row_getEventDetails = mysql_fetch_assoc($getEventDetails); 
$totalRows_getEventDetails = mysql_num_rows($getEventDetails); 

mysql_select_db($database_spectac, $spectac); 
$query_getFileList = "SELECT * FROM tbl_eventFiles"; 
$getFileList = mysql_query($query_getFileList, $spectac) or die(mysql_error()); 
$row_getFileList = mysql_fetch_assoc($getFileList); 
$totalRows_getFileList = mysql_num_rows($getFileList); 


?> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script> 
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
</script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script> 
    $(function() { 
     $("#datepicker").datepicker(); 
     $(eventDate).datepicker({dateFormat: 'yy-mm-dd'}) 
    }); 
    </script> 
    <? if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { ?><div align="center" >Event Updated <br> 
     <a href="http://spectaculareventcenter.com/2012/index.php/component/content/article/2-uncategorised/22-editevents">Go back to event list. </a></div><? } ?> 
<form method="post" name="form1" action="<?php echo $editFormAction; ?>"> 
    <table align="center"> 
    <tr valign="baseline"> 
     <td nowrap align="right">ID:</td> 
     <td><?php echo $row_getEventDetails['eventId']; ?></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event Date:</td> 
     <td><input type="text" name="eventDate" value="<?php echo htmlentities($row_getEventDetails['eventDate'], ENT_COMPAT, ''); ?>" size="32" id="datepicker"></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event Active:</td> 
     <td><p> 
     <label> 
      <input <?php if (!(strcmp($row_getEventDetails['eventActive'],"1"))) {echo "checked=\"checked\"";} ?> type="radio" name="eventActive" value="1" id="RadioGroup1_0"> 
      Yes</label> 
     <br> 
     <label> 
      <input <?php if (!(strcmp($row_getEventDetails['eventActive'],"0"))) {echo "checked=\"checked\"";} ?> type="radio" name="eventActive" value="0" id="RadioGroup1_1"> 
      No</label> 
     <br> 

     </p></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event Cost:</td> 
     <td><input type="text" name="eventCost" value="<?php echo htmlentities($row_getEventDetails['eventCost'], ENT_COMPAT, ''); ?>" size="32"></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event Name:</td> 
     <td><input type="text" name="eventName" value="<?php echo htmlentities($row_getEventDetails['eventName'], ENT_COMPAT, ''); ?>" size="32"></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Display Name:</td> 
     <td><input type="text" name="eventFriendlyName" value="<?php echo htmlentities($row_getEventDetails['eventFriendlyName'], ENT_COMPAT, ''); ?>" size="32"></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event Desc:</td> 
     <td><textarea cols=32 rows=5 name="eventDesc" value="<?php echo htmlentities($row_getEventDetails['eventDesc'], ENT_COMPAT, ''); ?>" size="32"></textarea></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">Event File:</td> 
     <td><select name="eventFile"> 
     <?php 
do { 
?> 
     <option value="<?php echo $row_getFileList['fileName']?>"<?php if (!(strcmp($row_getFileList['fileName'], $row_getEventDetails['eventFile']))) {echo "selected=\"selected\"";} ?>><?php echo $row_getFileList['fileName']?></option> 
     <?php 
} while ($row_getFileList = mysql_fetch_assoc($getFileList)); 
    $rows = mysql_num_rows($getFileList); 
    if($rows > 0) { 
     mysql_data_seek($getFileList, 0); 
     $row_getFileList = mysql_fetch_assoc($getFileList); 
    } 
?> 
     </select> 
     <a href="http://spectaculareventcenter.com/2012/customCode/file_upload.php" target="_blank">Upload File</a></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap align="right">&nbsp;</td> 
     <td><input type="submit" value="Update record"></td> 
    </tr> 
    </table> 
    <p> 
    <input type="hidden" name="MM_update" value="form1"> 
    <input type="hidden" name="eventId" value="<?php echo $row_getEventDetails['eventId']; ?>"> 
    </p> 
</form> 

<?php 
mysql_free_result($getEventDetails); 

mysql_free_result($getFileList); 
?> 

回答

0

輸入字符串使用nl2br之前將其存儲到數據庫中。至於自動鏈接,爆炸由空格輸入字符串,遍歷所有單詞(或使用array_walk())並在其上調用

filter_var($yourstring, FILTER_VALIDATE_URL); 

。如果它返回true,那麼只需在它周圍添加html鏈接標記並替換數組元素。最後,implode你的數組以獲得用於存儲在數據庫中的明文。

Here is a function for adding link I found with quick google search.

至於你的第三點意見,如果你想能夠編輯在textarea的我建議你不要將其插入到數據庫之前使用nl2br原文。相反,保存原始文本,並且只在顯示數據到頁面時才使用nl2br,所以只要你回顯​​它。

+0

如何在輸入字符串中添加nl2br?你能告訴我什麼路線和我怎麼做?我認爲你只是把nl2br放在這之前不是嗎? GetSQLValueString($ _POST ['eventDesc'],「text」)就像之前的$ _POST部分是正確的? – user2997536

+0

好的,我得到了換行符爲我工作,但我不知道你剛剛對鏈接說什麼。您能否重新緩慢解釋並且非常詳細?當我這樣做時,我是一個完全白癡,需要時間來學習一些東西,比如這個:-( – user2997536

+0

還有一個問題:當我回去嘗試編輯它時,我之前輸入的是textarea字段不顯示(所有其他字段做!)。我怎樣才能使textarea字段數據顯示? – user2997536