如何使用从ajax请求获得的变量以及其他变量并在函数中使用它们?

时间:2018-06-08 09:44:57

标签: javascript php ajax

我正在使用ajax请求来显示我从javascript弹出窗体中获得的一些用户输入变量。我有我的index.php,它包含弹出窗体和ajax请求,源代码如下:

index.php

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog"> 
    <div class="modal-dialog">

   <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="exampleModalLabel">Input parameters</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="recipient-name" class="form-control-label">Enter you Name:</label>
                        <input type="text" class="form-control" id="recipient-name">
                    </div>
                    <div class="form-group">
                        <label for="message-text" class="form-control-label">Enter your message:</label>
                        <input type="text" class="form-control" id="message-text">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">
                    Close
                </button>
                <button id="paramsOkay" type="button" class="btn btn-primary">
                    Okay
                </button>
            </div>
        </div>

    </div>
  </div>

</div>



   <div id="content"></div>

    <script>

    $(document).ready(function(){

        $('#myModal').on('click','#paramsOkay', function (e) {
          //var x = console.log($('#recipient-name').val());
          //var y = console.log($('#message-text').val());
           var x = $('#recipient-name').val();
           var y = $('#message-text').val(); 
           var xy = (x+","+y);
             $.ajax({
             type:"POST",
             url:"action.php",
             data:{name:xy},
             success:function(result){
             $("#content").html(result);
             }
             });

    //console.log(e);
       });

    })

    </script>




    </body>
</html>

和我的action.php用于将变量显示为php变量

action.php的

<?php

$string = $_POST["name"];
echo $string;//string that contains the name and the message ina single line as follows (name,message) 
echo "<br>";

$name = strstr($string, ',',true);
echo $name; // prints just the name 

echo "<br>";

$message = substr($string, strpos($string, ",") + 1);    
echo $message;// prints just the message without the semicolon


?>

我想做的不是回应来自action.php的变量我想在函数中使用它们以及我在index.php中的一些其他变量如何将它们传递给我的index.php insted只是贬低他们?

0 个答案:

没有答案