简体   繁体   English

PHP textarea内容转换为Javascript

[英]PHP textarea content to Javascript

I want to pass a textarea content to JavaScript function , 我想将textarea内容传递给JavaScript函数,

<body>

////////////////////////some html //////////////////

<?php

if(isset($_POST['srchsubmet']))
{
   $query = $_POST['matrit'];

   $sqll = "SELECT * FROM mem   WHERE `name` LIKE '%".$query."%' " ;    
   $raw_results = mysqli_query( $con, $sqll)  or die(mysql_error());

   if(mysqli_num_rows($raw_results) > 0)
   {
      while($results = mysqli_fetch_array($raw_results))
      { 
            $srch_id = $results['id']; 
            $srch_name = $results['username'];
            $srch_language = $results['language'];
            $srch_age = $results['age'];
            $srch_gender = $results['gender'];
            $srch_country = $results['country'];
            $srch_photo = $results['image'];  

echo '<div class="ana">';
echo '<div id="pho"> <img src="', $www_root, '/', $srch_photo, '" >;</div> ';
echo '<div id="info">-Name : '.$srch_name.' <br>';
echo '-Langue:'.$srch_language.'<br>';
echo '-Age:'.$srch_age.'<br>';
echo '-Contry : '.$srch_country.'';
echo '</div>';
echo '<div id="icon-call">call</div>';
echo '<div id="inter"> '.$srch_interest.' </div>';
echo '<div class="icon-message">mesage</div>';
echo '<div id="textmsg"><form method="post"><textarea id="taa"></textarea><button id="pmBtn" onClick="nn(\''.$srch_name.'\',\''.$uname.'\', taa)">Send</button></form> </div>';
echo '</div> <!-- end of ana -->                                            ';
        }


}      
  else{ // if there is no matching rows do following
            echo "No results";  }
}

?>

//////////////////////////////////some html////////////////////

<script type="text/javascript"> 
function nn(tuser, fuser, ta) {

  alert(fuser + " send to   " + tuser + " he say " +ta);
}

</script>
</body>
</html>

when you type someone's name in search box all user's profile with that name appear , and when you want to send to someone message you click on message and text area appear type a message and hit send , the problem is the text that you enter in text area doesn't pass to a function that you cant store it in db or do what ever you want with it as example i display the sender's name , profile's name and the message ,but the message doesn't pass, when i use var ta = document.getElementById("ta").value; 当您在搜索框中键入某人的名字时,将出现具有该名字的所有用户个人资料,并且当您要发送给某人的消息时,单击消息和文本区域,然后键入一条消息并单击send,问题就是您在文本中输入的文本该区域不会传递给您无法将其存储在db中的功能,也无法执行任何操作,例如,当我使用var ta = document.getElementById("ta").value;时,我会显示发件人的姓名,个人资料的姓名和消息,但消息不会传递var ta = document.getElementById("ta").value; for example there is two profile when i want to send message to the second profile's owner when i wrote the message and hit send it doesn't send anything because the var ta = document.getElementById("ta").value; 例如,当我想发送消息给第二个配置文件的所有者时,我有两个配置文件,当我写消息并点击发送时,它不会发送任何内容,因为var ta = document.getElementById("ta").value; pointer to the first profile's text area, so the only way to solve this problem is to pass that variable from php using something like that <div id="textmsg"><form method="post"><textarea id="taa"></textarea><button id="pmBtn" onClick="nn(\\''.$srch_name.'\\',\\''.$uname.'\\',\\'taa\\')">Send</button></form> i don't know why it can't pass the message. 指向第一个配置文件的文本区域的指针,因此解决此问题的唯一方法是使用类似<div id="textmsg"><form method="post"><textarea id="taa"></textarea><button id="pmBtn" onClick="nn(\\''.$srch_name.'\\',\\''.$uname.'\\',\\'taa\\')">Send</button></form>东西从php传递该变量。 <div id="textmsg"><form method="post"><textarea id="taa"></textarea><button id="pmBtn" onClick="nn(\\''.$srch_name.'\\',\\''.$uname.'\\',\\'taa\\')">Send</button></form>我不知道为什么它无法传递消息。

Try this: 尝试这个:

function test(a, b, c) { 
 alert(a + " live in " + b + " say " + document.getElementById(c).value); 
} 

I'd sooner use this.previousSibling.value: 我将尽快使用this.previousSibling.value:

<!doctype html>
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function test(a, b, c) {
 alert(a + ' live in ' + b + ' says ' + c + ".");
}
</script>
</head>
<body>
<?php
$a = 'Father Christmas';
$b = 'America';
echo sprintf('<textarea>Hello</textarea><button onclick="test(%s,%s,this.previousSibling.value)">LINK</button>',htmlentities(json_encode($a)),htmlentities(json_encode($b)));
?>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM