正确缩放html,并使用javascript/jquery添加新信息


scale html properly and appending new information with javascript/jquery

我对javascript/jquery非常不熟悉,在思考这段代码时遇到了困难。我使用PHP做了我想做的事情,但当添加了一定数量的新条目时,它会导致html混乱。

我在PHP中的代码:

 for($i = 0; $i < sizeof($educationArray[0]); $i++)
    {   
        echo "<div style='"float:left; margin-right:50px; margin-bottom:30px; '">";
        echo "<label>Institution Name</label>";
        echo "<span>".$newarray[$i]['Institution']."</span>";
        echo "<label>Location</label>";
        echo "<span>".$newarray[$i]['Location']."</span>";
        echo "<label>Start Date</label>";
        echo "<span>".$newarray[$i]['StartDate']."</span>";
        echo "<label>End Date</label>";
        echo "<span>".$newarray[$i]['EndDate']."</span>";
        echo "<label>Degree</label>";
        echo "<span>".$newarray[$i]['Degree']."</span>";
        echo "<label>Date Received Degree</label>";
        echo "<span>".$newarray[$i]['DegreeDate']."</span>";
        echo "<label>GPA</label>";
        echo "<span>".$newarray[$i]['GPA']."</span>";
        echo "</div>";
    }
    ?>

我希望能够在javascript中自动完成这项工作。我必须向数据库添加新信息的代码是:

    <h3 style="float:left;">Add Education</h3>
 <button style="float:left;" class="editInfoButtons" id="AddEducationButton"></button>
 <div style="float:left;" id="EducationInfo" class="modal-basicInfo">
           <div style="height:600px;width:260px;">
                <a title="Close" class="close">x</a>
 <form method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?user=" . $userInfo['Id']; ?>" >

    <label for="InstitutionName">Institution Name: </label>
    <input type="text" name="schoolName" id="InstitutionName" />

    <label for="InstitutionLocation">Location: </label>
    <input type="text" name="schoolLocation" id="InstitutionLocation" />
  <br /><br />
    <label for="DatesAttended">Dates of Attendance</label>
    <input type="date" id="DateBegin" name="dateBegin"  />-<input type="date" name="dateEnd" id="DateEnd" />
    <br /><br />
    <label for="DegreeEarned">Degree Earned</label>
    <input id="DegreeEarned" name="DegreeEarned" type="text" />
    <label for="DateEarned">Date Earned: </label>
    <input style="float:left;" name="DateEarned" id="DateEarned" type="date" />
        <br />
        <br />
    <label for="GPA">GPA: </label>
    <input id="GPA" name="GPA" type="text" />

 <input type="submit" id="AddSchools" name="AddSchools" class="button" value="Add Institute" />
</form>
</div>

快速描述发生了什么:当他们点击按钮时,会出现一个div,他们可以在其中添加信息。一旦他们输入信息并点击submit,它就会被保存到数据库中,div就会消失,页面会被重新加载以显示添加的新信息,从而向下推送旧信息。

有关于使用Javascript/Jquery执行此操作的指导吗?

在jQuery中,您可以使用ajax请求,然后进行成功的回调函数,并在您的div中进行预处理(将新插入的信息放在顶部,将旧信息向下推)。

让我先介绍一下ajax。。。

$("#AddSchools").click(function(){
    $.ajax({
        type: "POST",
        url: "insert-to-db.php",
        data: { schoolName:$("#InstitutionName").val(),
                schoolLocation:$("#InstitutionLocation").val(),
                DateBegin:$("#DateBegin").val(),
                DegreeEarned:$("#DegreeEarned").val(),
                DateEarned:$("#DateEarned").val(),
                GPA:$("#GPA").val()
                } ,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result){
         $("div").prepend("<label>Institution Name</label><br>"+$("#InstitutionName").val()+"(and so on...)");
        }
    });
});