gpt4 book ai didi

php - 向 PHP 发送动态数据

转载 作者:行者123 更新时间:2023-12-01 05:18:14 25 4
gpt4 key购买 nike

我有表格,您可以在其中查看工作经历,该表格显示在三个输入的行中:职位名称、工作持续时间和工作地点。您可以添加就业,这会添加一组空的输入,您可以在其中添加更多数据。

我通过 AJAX 将 HTML 表单发送到 PHP 脚本,它分别发送具有相同名称的所有输入集,因此不在数组中。在 PHP 方面,我通过将 post 变量放入数组中对它们进行 json 编码。 $json_data

问题在于 PHP 脚本仅保存最后一组输入。因此,如果您输入 3 个职位,它只会保存最后一个职位。

<!-- employment row -->

<p class="mt-3 text-left"> Employment history </p>
<div class="row employment_block">

<div class="col-md-3">
<input type="text" name="job_title[]" placeholder="Job title">
</div>

<div class="col-md-3">
<input type="text" name="job_duration[]" placeholder="Duration (i.e. 3 months / 1 year)">
</div>

<div class="col-md-3">
<input type="text" name="job_location[]" placeholder="Country">
</div>
<div class="col-md-3 text-center">
<button href="#" class="able_btn m-0 w-100 disabled" disabled aria-disabled="true" style="opacity:0.5"> remove </button>
</div>

</div>

<div class="text-center mb-3 mt-3">
<a class="add_employment" href=""> Add employment </a>
</div>



<!-- JS -->

$(document).ready(function(){

$(".account_cv_update_form").submit(function(evt){
evt.preventDefault();

var formData = new FormData($(this)[0]);
$.ajax({
url: 'handlers/cv_update.php',
type: 'POST',
data: formData,
async: false,
cache:false,
contentType: false,
processData: false,
dataType: "json",
success: function (response) {

switch(response.message){
case 'success': success();
break;
case 'error': error();
break;

}

}
});
return false;
});

});



$('.add_employment').click(function(evt) {
evt.preventDefault();

$('.employment_block:last').after('<div class="row employment_block"><div class="col-md-3"><input type="text" name="job_title" placeholder="Job title"></div><div class="col-md-3"><input type="text" name="job_duration" placeholder="Duration (i.e. 3 months / 1 year)"></div><div class="col-md-3"><input type="text" name="job_location" placeholder="Country"></div><div class="col-md-3 text-center"><button class="able_btn m-0 w-100 remove_employment"> remove </button></div></div>');
});



<!-- PHP code -->

$json_data = array(
'job_title' => $job_title,
'job_location' => $job_location,
'job_duration' => $job_duration );


$cv_data = json_encode($json_data);

$stmt = $dbh->prepare("UPDATE table SET profile_picute=:profile_picute, first_name=:first_name, last_name=:last_name, phone_number=:phone_number, email_address=:email_address, data=:data WHERE id=:id");

$stmt->bindParam(':profile_picute', $new_file_name);
$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':phone_number', $phone_number);
$stmt->bindParam(':email_address', $email_address);
$stmt->bindParam(':data', $cv_data);
$stmt->bindParam(':id', $user_id);


if($stmt->execute()){
$response["message"] = 'success';
}else{
$response["message"] = 'error';
$errors++;
}

echo json_encode($response);
exit();

最佳答案

正如我在评论区所说:

I don't know JS that much, but the inputs in $('.employment_block:last').after..... do not have arrays [] as you have for the inputs above that, so I'm not sure if they should have them also.

操作:

Yes, I just checked it. you were right. create an answer and i'll mark it correct. – bob

所以我是对的,我今天学到了一些 JS ;-)

因此这一行:

$('.employment_block:last').after('<div class="row employment_block"><div class="col-md-3"><input type="text" name="job_title" placeholder="Job title"></div><div class="col-md-3"><input type="text" name="job_duration" placeholder="Duration (i.e. 3 months / 1 year)"></div><div class="col-md-3"><input type="text" name="job_location" placeholder="Country"></div><div class="col-md-3 text-center"><button class="able_btn m-0 w-100 remove_employment"> remove </button></div></div>');
});

应读取为并添加 [] 作为输入:

$('.employment_block:last').after('<div class="row employment_block"><div class="col-md-3"><input type="text" name="job_title[]" placeholder="Job title"></div><div class="col-md-3"><input type="text" name="job_duration[]" placeholder="Duration (i.e. 3 months / 1 year)"></div><div class="col-md-3"><input type="text" name="job_location[]" placeholder="Country"></div><div class="col-md-3 text-center"><button class="able_btn m-0 w-100 remove_employment"> remove </button></div></div>');
});

为了将它们视为数组。

关于php - 向 PHP 发送动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47455781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com