简体   繁体   English

如何通过单击按钮(不提交表单)将Textbox值获取到同一php文件中的php变量?

[英]How to get Textbox value to a php variable within same php file with a button click (without submitting the form)?

I am trying to get the textbox value with a button click (without form submission) and assign it to a php variable within the same php file. 我试图通过单击按钮(不提交表单)获取文本框值,并将其分配给同一php文件中的php变量。 I tried AJAX, but, I don't know where I am making mistake. 我尝试过AJAX,但是,我不知道在哪里出错。 Sample code: File name: trialTester.php 示例代码:文件名:trialTester.php

<?php
 if(!empty($_POST))
 echo "Hello ".$_POST["text"];
 ?>
<html>
  <head>
   <title> Transfer trial </title>
   <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
   <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
   <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 </head>
<body>
 <input type="textbox" id="scatter" name="textScatter">
 <button id="inlinesubmit_button" type="button">submit</button>
 <script>
  $(document).ready(function(){
   function submitMe(selector)
   {
    $.ajax({
      type: "POST",
      url: "",
      data: {text:$(selector).val()}
    });

   }
   $('#inlinesubmit_button').click(function(){
    submitMe('#scatter');
   });
  });
 </script>
</body>
</html>
  1. You forgot to close $(document).ready(function(){ . 您忘记关闭$(document).ready(function(){
  2. Your AJAX posts text parameter, but in your PHP code you're accessing textScatter . 您的AJAX发布了text参数,但是在您的PHP代码中,您正在访问textScatter Change that to text 将其更改为text
  3. It should be <input type="text" id="scatter" name="textScatter"> , there is no textbox type. 它应该是<input type="text" id="scatter" name="textScatter"> ,没有textbox类型。
  4. You make an ajax request, so when it completes you need to display the results, eg: 您发出了ajax请求,因此在完成请求后,您需要显示结果,例如:

- -

function submitMe(selector)
{
    $.ajax({
    type: "POST",
    url: "",
    data: {text:$(selector).val()},
    success: function(newHtml) {
       $("body").html(newHtml);
    }
});

When you do $.ajax you specify the names of the POSTed parameters yourself, name="textScatter" is not taken into account. 当您执行$.ajax您自己指定POSTed参数的名称,不会考虑name="textScatter"

By using ajax you can get the post value but by replacing the existing content. 通过使用ajax,您可以获取帖子值,但可以替换现有内容。

See the code below, as all the content is pesent in the body tag, so we will replace the content present in this body through the response of ajax. 请参见下面的代码,因为所有内容都存在于body标记中,所以我们将通过ajax的响应替换此主体中存在的内容。

Copy & paste this code & let me know if you have any query. 复制并粘贴此代码,如果您有任何疑问,请与我们联系。

<?php
if(!empty($_POST))
echo "Hello ".$_POST["text"];
?>
<html>
    <head>
        <title> Transfer trial </title>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    </head>
<body>
<input type="textbox" id="scatter" name="textScatter">
<button id="inlinesubmit_button" type="button">submit</button>

<script>
$(document).ready(function(){
   function submitMe(selector)
   {
    $.ajax({
    type: "POST",
    url: "",
    data: {text:$(selector).val()},
    success: function(data){
            alert(data);
            $("body").html(data);
        }
    });

   }
   $('#inlinesubmit_button').click(function(){
    submitMe('#scatter');
   });
});
</script>
</body>
</html>

Hope, this may be helpful for you. 希望对您有帮助。

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

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