简体   繁体   中英

PHP textarea content to Javascript

I want to pass a textarea content to JavaScript function ,

<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; 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; 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.

Try this:

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

I'd sooner use 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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