如何将许多ajax结果传递给不同的输入值?

时间:2017-07-28 03:17:36

标签: javascript php html ajax

我的ajax返回结果是四个值。我想将这些值分配给四个输入值。这是我的ajax代码:

$.ajax({
 type:"POST",
 url:"modify_cbndtb.php",
 data: {cabinetNum:id},
 success:function (res) 
 {
 } 
});

modify_cbndtb.php代码:

if(isset($_POST['cabinetNum']))
{   
 $q=$_POST["cabinetNum"];
 $sql="select num1,num2,num3,num4 from  hpc WHERE sysid= '".$q."';";
 $sel = $conn->query($sql);
}

我的HTML代码:

<div id="content" class="content">
1U:<input type="text" id="1U" value="">11U:<input type="text" id="11U" value=""><br />
2U:<input type="text" id="2U" value="">12U:<input type="text" id="12U" value=""><br />
</div>

1U.value应为num1。 2U.value应为num2。 3U.value应为num3。 4U.value应为num4。但我不知道如何实现。谁能帮帮我?

2 个答案:

答案 0 :(得分:0)

我想你可以试试这个

ALTER PROCEDURE [dbo].[GetOneWeekRosters] 
    -- Add the parameters for the stored procedure here
    @Date varchar(20), 
    @DepartmentID int,
    @RosterID int,
    @RosterWeekID int,
    @Active bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT distinct n.EmployeeID, n.FirstName, n.Surname, n.DepartmentID, n.InstitutionID, 
    n.ContractedHours, n.ReportingOrder, n.RoleName, 
    ISNULL(RosterLines.Notes, 'no notes') as Notes, ISNULL(RosterLines.ActivityTypeID, 8) as ActivityTypeID, 
    ISNULL(ActivityTypes.ActivityTypeShort, 'NW') as ActivityTypeShort, 
    ISNULL(RosterLines.RosterHourValue,0) as RosterHourValue, 
    ISNULL(RosterLines.RosterLineID, 0) as RosterLineID, ISNULL(RosterLines.PositionID, 1) as PositionID, 
    ISNULL(RosterLines.RosterShiftStart, m.DateDim) as ShiftStart, 
    RosterAreas.RosterAreaName, RosterShifts.RosterShiftName, 
    @RosterWeekID as RosterWeekID, @RosterID as RosterID,
    ISNULL(RosterLines.RosterShiftFinish, m.DateDim) as ShiftFinish, ISNULL(RosterLines.RosterShiftID, 0) as RosterShiftID, 
    ISNULL(RosterLines.RosterLineID, 0) as RosterLineID, RosterLines.RosterAreaID, 
    m.DateDim as DateEntry from 
    (select DateDimension.DateDim, DateDimension.DayOfWeek_ShortName, DateDimension.Calendar_DayOfMonth 
    from DateDimension where DateDimension.DateDim between Convert(datetime,@Date,103) and (Convert(datetime,@Date,103) + 6)) m 
    cross join 
    (select Employees.EmployeeID, Employees.FirstName, Employees.Surname, Employees.EmploymentTypeID, Employees.ContractedHours, 
    Employees.RoleName, Employees.Active, EmploymentTypes.EmploymentTypeName, EmploymentTypes.ReportingOrder, 
    EmpToDepts.DepartmentID, EmpToDepts.InstitutionID
    from Employees left join EmploymentTypes on Employees.EmploymentTypeID = EmploymentTypes.EmploymentTypeID 
    left join EmpToDepts on Employees.EmployeeID = EmpToDepts.EmployeeID 
    where EmpToDepts.DepartmentID = @DepartmentID and Employees.Active = @Active) n 
    left join RosterLines on n.EmployeeID = RosterLines.EmployeeID and n.DepartmentID = RosterLines.DepartmentID 
    and m.DateDim = RosterLines.DateEntry left join ActivityTypes on RosterLines.ActivityTypeID = ActivityTypes.ActivityTypeID 
    left join RosterAreas on RosterLines.RosterAreaID = RosterAreas.RosterAreaID 
    left join RosterShifts on RosterLines.RosterShiftID = RosterShifts.RosterShiftID
    order by n.DepartmentID, n.ReportingOrder, n.Surname, n.FirstName
END

和AJAX

modify_cbndtb.php

  if(isset($_POST['cabinetNum']))
  {   
     $q=$_POST["cabinetNum"];
     $sql="select num1,num2,num3,num4 from  hpc WHERE sysid= '".$q."';";
     $sel = $conn->query($sql);
     $arr = $sel->fetch(PDO::FETCH_ASSOC);
     $data = json_encode($arr);
     echo $data;
  }

我希望它可以帮到你

答案 1 :(得分:0)

试试这个

modify_cbndtb.php

this

你的表格

<?php
if(isset($_POST['action']))
{   
    $q=$_POST["cabinetNum"];
    $sql="select num1,num2,num3,num4 from  hpc WHERE sysid= '".$q."';";
    $sel = $conn->query($sql);

    while($row = $sel->fetch_assoc()){
            echo $row['num1'].",".$row['num2'].",".$row['num3'].",".$row['num4'];
    }
}
?>