从php中获取ajax的许多值 [英] Retrieve many values with ajax from get in php

查看:76
本文介绍了从php中获取ajax的许多值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有3个文件ajax.js,index.php和retrieve.php。

Here I have 3 file ajax.js, index.php, and retrieve.php.

$('#button').click(function(){
   var string=$('#string').val();
   $.get('php/retrieve.php',{input: string} ,function(data){ 
       $('#feedback').text(data);                           
   }); 
});

index.php

index.php

<html lang="fr">
<head><title></title>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>


    <input id="string" type="text" name="string"/><input id="button" type="button" value="Go"/>
    <input id="string2" type="text" name="string"/><input id="button2" type="button" value="Go"/>
    <div id="feedback"></div>
    <div id="feedback2"></div>

    <script type="text/javascript" src="js/jquery-2.1.4.js"></script> 
    <script type="text/javascript" src="js/ajax.js"></script>
</body>
</html>

retrieve.php

retrieve.php

<?php
    if(isset($_GET['input'])){
        $string=$_GET['input'];
        echo strrev($string);
    }
?>

在retrieve.php中,我从ajax中检索输入值。 JS。 ajax.js从index.php中检索值。但是在这里它只有
检索一个值(从< input id =stringtype =textname =string/>
我想从index.php中获取很多值(例如< input id =button2type =buttonvalue =Go/>
我该怎么做?

In retrieve.php, I retrieve the input value from ajax.js. ajax.js retrieve the value from index.php. But here it only retrieves one value (from <input id="string" type="text" name="string"/>) I want to retrieve many values from index.php (for example <input id="button2" type="button" value="Go"/>) How can I do that ?

推荐答案

您可以创建一个更通用的jQuery函数,这两个函数都可以工作。首先,让我们更正您的HTML以包含适当的表单标签和输入的正确命名:

You can create a more generic jQuery function that will work in both cases. First, let's correct your HTML to include proper form tags and proper naming of the inputs:

<html lang="fr">
<head><title></title>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>

    <form name="myForm" action="retrieve.php">
        <input id="string" type="text" name="string1"/>
        <input id="button" type="button" value="Go"/>
        <input id="string2" type="text" name="string2"/>
        <input id="button2" type="button" value="Go"/>
    </form>
    <div id="feedback"></div>
    <div id="feedback2"></div>

    <script type="text/javascript" src="js/jquery-2.1.4.js"></script> 
    <script type="text/javascript" src="js/ajax.js"></script>
</body>
</html>

现在您可以使用一个函数来处理按钮点击:

Now you can use one function to handle either button click:

$('[type="button"]').click(function(){
   var currentinput = $(this).prev('input').attr('name'); // string1 or string 2
   var string = $(this).prev('input').val();
   $.get('php/retrieve.php',{input: string} ,function(data){
       if('string1' == currentInput) {
           $('#feedback').text(data);
       } else {
           $('#feedback2').text(data);
       }                           
   }); 
});

您可能希望每个表单只有一个按钮,因此您应该将表单分开与他们自己的投入。即使如此,上面的jQuery也可以工作,如果需要,以后可以添加更多类似的表单。

You will likely want to only have one button per form, so you should really separate the forms, each with their own inputs. Even so, the above jQuery will work and you can add more, similar, forms at a later date should you need to.

这篇关于从php中获取ajax的许多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆