普通网友 2013-05-02 13:39
浏览 14

使用ajax传递给php的值

I have the following script :

<script type="text/javascript" >

$('form').each(function() {
    $(this).on('submit', function() {

var first_firstname = $(".first_firstname", this).val();
var first_lastname = $(".first_lastname", this).val();
var second_firstname = $(".second_firstname", this).val();
var second_lastname = $(".second_lastname", this).val();
var TeamName = $(".TeamName", this).val();
var dataString = 'first_firstname='+ first_firstname + '&first_lastname=' + first_lastname +
'&second_firstname=' + second_firstname + '&second_lastname=' + second_lastname + '&TeamName=' + TeamName;

 $.ajax({
type: "POST",
url: "data.php",
data: dataString,
success: function(){
window.setTimeout(function(data)
{
$('#propspectDiv').html('Team Name Added!');
$('#data').css("display","block");
$('#data').html(data);
}, 2000);
}

});

return false;
});

</script>

And the following php that generates a number of forms on a page using mysql database

<?php    

echo '<table class="greensmalltbl" cellspacing="10px" cellpadding="5px"><div id="propspectDiv"></div>';

    for ($i=1,  $o=$totalEntrants; $i<=$half; $i++, $o=$o-1) {

    $formid = $i;


echo "<div style='border:3px;'><form action='' method='post'>
<tr><td><input type='text' name='first_firstname' id='first_firstname' value='$firstName[$i]' />
<input type='text' name='first_lastname' id='first_lastname' value='$lastName[$i]' />
  Skill Level : ".$skill[$i]."</td></tr>";
echo "<tr><td>WITH</td></tr>";
echo "<tr><td><input type='text' name='second_firstname' id='second_firstname' value='$firstName[$o]' />
<input type='text' name='second_lastname' id='second_lastname' value='$lastName[$o]' /> Skill Level ".$skill[$o]."</td></tr>";
echo "<tr><td>Enter Team Name : <input type='text' name='TeamName' id='TeamName' value='' />
<input type='submit' name='submit' value='Submit'></form></td></tr>";

    }

echo '</table>';

?>

I want to update the db table with the TEAM NAME in each form

The problem is only the first forms input is passed all other forms do nothing I have tried a number of variations to the ajax code but none have worked. Can anyone find the problem here

  • 写回答

1条回答 默认 最新

  • dousao6260 2013-05-02 15:33
    关注

    This line will not return the form.

    var parent = $(this).parent('form');
    

    because your submit button is wrapped inside tr and td tags. Either get rid of those tags (they are invallid anyway, because your are not using them in a table), or update your code to:

    var parent = $(this).closest('form');
    

    closest() searches its way up to all the ancestors of an element, and will return the first match of the selector. Check out the documentation here: http://api.jquery.com/closest/

    Or, if you only have a single form in your page, you could just go:

    var parent = $('form');
    

    :: EDIT ::

    OK. Forget all of the above. Seems like you are not even using the parent variable later in the code. A more important problem is that even though you are catching the Click event on the form submit button, what you probably really want to do is catch the submit-event of the form.

    So change your first line of code to this:

    $('form').on('submit', function() {
    

    Also, in your HTML, your code is invalid.

    <form action'' method='post' id='$formid'>
    

    action'' should be action = ''

    Chances are this doesn't really fix your problem, because there might be more errors. Next time, try to validate your code before posting a question.

    :: EDIT ::

    Last edit, I promise. I quickly went trough your code again, and it seems you will have multiple forms. this means, that you will get elements in different forms with the same id's. An id should be unique for troughout the page. So when you try to get a value like this $("#second_firstname").val(); that won't work. because jQuery doesn't know what element you mean, so all elements that can appear multiple times in a page need to have a class and CAN NOT have an id.

    You could then loop trough your forms by changing things to:

    $('form').each(function() {
        $(this).on('submit', function() {
    
            var first_firstname = $(".first_firstname", this).val(); // . instead of # and use 'this' as context
    
            // and so on..
            // the rest of your code here.
        }
    });
    

    table with forms can be seen here

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题