div 项目的 php onclick 将帖子发送到 url

Posted

技术标签:

【中文标题】div 项目的 php onclick 将帖子发送到 url【英文标题】:php onclick of div item sent post to url 【发布时间】:2016-09-29 08:59:58 【问题描述】:

我有一个名为 userOnline 的 div,它包含许多来自服务器的联系人。 当我单击 div 中的任何项目时,我想将项目发布到 url。 如何使它成为可能?请用示例代码说明。

<?php
session_start();

//mail("ifaisal24@gmail.com","hhhhhh",print_r($_REQUEST,TRUE));
define('HOST','xxxxxxxxxxxxx');
define('USER','xxxxxxxxxxxxxx');
define('PASS','xxxxxxxxxxxxxxx');
define('DB','xxxxxxxxxxxxxxxxx');

$con = mysqli_connect(HOST,USER,PASS,DB);
require_once 'include/DB_Functions.php';
 if (!isset($_SESSION['email']))
     ?>
     <script>
     window.location = 'login2.php';
     </script>
 <?php
 


$db = new DB_Functions();

$response = array("error" => FALSE);
if (isset($_POST['sender_email'])&& isset($_POST['reciver_email'])&& isset($_POST['data'])) 

    // receiving the post params

     $sender_email = $_SESSION['email'];
     $_POST['sender_email']=$_SESSION['email'];
     $reciver_email = $_POST['reciver_email'];
     $data =$sender_email.' : '. $_POST['data'];

        ///echo $sender_email;
        //die();

        // create a new chat
        $user_data = $db->storeData($data,$sender_email,$reciver_email);
        //die;
        if ($user_data) 
            // chat stored successfully
            $response["error"] = FALSE;
           // $response["uid"] = $user_data["unique_id"];
            $response["user_data"]["sender_email"] = $user_data["sender_email"];
            $response["user_data"]["reciver_email"] = $user_data["reciver_email"];
            $response["user_data"]["data"] = $user_data["data"];
            $response["user_data"]["created_at"] = $user_data["created_at"];

            echo json_encode($response);
        
                        

在这里我想发布我想在点击 div 项目时发送的数据我只想在此页面上发送sender_emailreciver_email 当我点击任何联系人时如何制作它请告诉我代码

          if (isset($_REQUEST['sender_email'])&& isset($_REQUEST['reciver_email'])&& isset($_REQUEST['fatch_server_data'])) 

                           $sender_email = $_REQUEST['sender_email'];
                         $reciver_email = $_REQUEST['reciver_email'];

         $sql = "select * from user_data WHERE (sender_email='$sender_email' AND reciver_email='$reciver_email') OR 
         (reciver_email ='$sender_email' AND sender_email='$reciver_email')" ;

        //$sql = "select * from user_data";
                          $res = mysqli_query($con,$sql)or mysql_error();
        $resultt = array(); 
            while($row = mysqli_fetch_array($res))
                array_push($resultt,
                array(

                'data'=>$row[3],
                'created_at'=>$row[4]
                //   'email'=>$row[3]
                ));
              
            //                     
 json_encode(array("resultt"=>$resultt));

mysqli_close($con);

?>

<?php
define('HOST','xxxxxxxxxxxxx');
define('USER','xxxxxxxxxxxxx');
define('PASS','xxxxxxxxxxxxxxxx');
define('DB','androidapi2');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from users";

$res = mysqli_query($con,$sql);
$result = array();

    while($row = mysqli_fetch_array($res))
    array_push($result,
    array(
     //  'id'=>$row[0],
      'email'=>$row[3], 
));
 
 json_encode(array("result"=>$result));
mysqli_close($con);

?>
<html>
<head>
<style>
#userschat  
    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 5px teal solid;
    height: 325px;
    width: 300px;

 overflow-y:scroll;

#usersOnLine  

    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 10px teal solid;
    height: 625px;
    width: 300px;
 overflow-y:scroll;

#allcon
    margin-bottom: 15px;
    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 10px teal solid;
    height: 40px;
    width: 300px;

#contact
    float:right;

#all
    float:left;

#data 
    width: 250px;
    height: 30px;
    border: 5px teal solid;
    float:left;
    margin-top:1px;


#send 
    border: 5px teal solid;
    width: 57px;
    height: 30px;
    float:right;
    margin:1px;

.container
    width:970px;
    height:auto;
    margin:0 auto;

#messageForm


.clickAble
                    display: inline-block;;
                    cursor:pointer; 
                

</style>
<meta http-equiv="refresh" content="600">
</head>
<body bgcolor="beige">>
<div class="container">
<h2><a href="logout2.php">Logout</a></h2>
<div id = "welcome"><h2>Welcome to Chat</h2>
<?php echo $_SESSION['email']; ?>
</div>

<div id = "all"> 
<h1> Chat Box</h1>
<div id="receiver">
                <h3>receiver</h3>
                <!-- NOTICE THAT THIS IS NEW NOW... -->
                <!-- IT'S IMPORTANT THAT YOU HAVE THIS PARAGRAPH EXACTLY AS IT IS HERE -->
                <p class='bubbled-data'></p>
            </div>
<div id="sender"><h3>sender</h3>
    <?php echo $_SESSION['email']; ?>
    </div>
<div id="userschat">
<h4 style = " margin-top: 4px; margin-left: 7px">
<?php
foreach($resultt as $key => $val)

    echo $val['data'];
    echo "<br>";
    echo $val['created_at'];   
    echo "<br>";
    echo "<br>";

 ?>
 </h4>
</div>
<form id="messageForm" action = "<?php $_PHP_SELF ?>" method = "POST">

    <input type="hidden" name="sender_email" id="sender_email" value="">
    <input type="hidden" name="reciver_email" id="reciver_email" value="">
<input type="text" name="data" id="data"   placeholder="data" onFocus="fun1(this)" onBlur="fun2(this)" required=""   />

    <input id="send" type="submit" value="Send">
</form>
</div> 

<div id = "contact">
<div id = "allcon"> 
  <h1 style = "  text-align: center; margin-top: 4px;">   All Contacts   </h1>
</div>
<div id="usersOnLine">
<h2 style = " margin-top: 4px; margin-left: 7px">
<?php
          foreach($result as $key => $val)
          echo "<span class='clickAble'>" . $val['email'] . "</span>";
          echo "<br>";
          echo "<br>";
         
 ?>
 </h2>
</div>
</div>

</div>

        <!-- javascript - JQUERY -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
       <script type="text/javascript">

       var objDiv = document.getElementById("userschat");
        objDiv.scrollTop = objDiv.scrollHeight;

    (function ($) 
        $(document).ready(function (e) 
            var receiver = $("#receiver");
            var clickAbles = $(".clickAble");

            receiver.find(".bubbled-data").text(localStorage.getItem("nameValue"));
           $("#reciver_email").val(localStorage.getItem("nameValue"));
            // BIND THE clickAble SPAN TO AN ON-CLICK EVENT...
            // SO THAT WITH EACH CLICK, YOU CAN SIMPLE APPEND THE CONTENT OF THAT SPAN TO THE RECEIVER DIV
            clickAbles.on("click", function (evt) 
                var nameValue = $(this).text();
                receiver.find(".bubbled-data").text(nameValue);

                $("#reciver_email").val(nameValue);
                localStorage.setItem("nameValue", nameValue);


            );

        );
    )(jQuery);
        </script>


</body>
</html>

【问题讨论】:

你到底卡在哪里了?您的代码使用 jQuery,如果您想执行 AJAX POST,请查看 jQuery $.post() 函数。 你能帮我一些代码如何制作吗? 【参考方案1】:

您需要使用 AJAX。这是一个例子:

$('.clickAble').on('click', function() 
  $.ajax(
   url: 'your_url',
   type: 'POST',
   data: $('#messageForm').serialize(),
   success: function(ret) 
        console.log('success', ret);
   ,
   error: function(err) 
    console.log('error', err); 
   
  );
  return false;
);

JSFiddle

参考:jQuery.ajax() 文档

【讨论】:

在 (function ($) $(document).ready(function (e)

以上是关于div 项目的 php onclick 将帖子发送到 url的主要内容,如果未能解决你的问题,请参考以下文章

使用html中的按钮onclick将php数组发送到javascript函数

PHP循环:围绕每三个项目语法添加一个div [重复]

将 PHP json_encode 数组发送到 jQuery

onClick() 事件适用于一个 div,但不适用于另一个。为啥?

onclick返回php文件后自动发送html表单

带有php echo的文本溢出div