gpt4 book ai didi

成功函数后javascript返回数据

转载 作者:行者123 更新时间:2023-11-29 02:49:39 26 4
gpt4 key购买 nike

我想使用 ajax 函数向 mysql 提交一个表单,但我的尝试在 mysql 表中给出了 NULL 结果。

这是我的 javascript :

function pesan()
{
email = $("#email").val();
from_nama = $("#from_nama").val();
from_phone = $("#from_phone").val();

$.ajax
({
url : "<?php echo site_url('kirim/undangan')?>/",
type: "POST",
dataType: "JSON",
success: function(data)
{
$('#alert').show();
$('#email'+data).html(data.email);
$('#from_nama'+data).html(data.from_nama);
$('#from_phone'+data).html(data.from_phone);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error upload data');
}

});
}

和形式:

        <h4 id="form">Data Personal</h4>

<div class="col-sm-4">
<input type="email" class="form-control input-lg" id="email" name="email" placeholder="Email" required>
</div>

<div class="col-sm-4">
<input type="text" class="form-control input-lg" id="from_nama" name="from_nama" placeholder="Nama" required>
</div>

<div class="col-sm-4">
<input type="number" class="form-control input-lg" id="from_phone" name="from_phone" placeholder="Phone" required>
</div>
<div>
<input type="hidden" name="id" id="id" />
</div>
<br>
<div class="row" align="center">

<button id="pesan" type="button" class="btn btn-download btn-md" onclick=pesan()>
<span class="glyphicon glyphicon-send" aria-hidden="true" ></span>
Pesan
</button>

这是 Controller :

function undangan()
{
$email = $this->input->post('email');
$from_nama = $this->input->post('from_nama');
$from_phone = $this->input->post('from_phone');

$data_user = array(

'email' => $email,
'name' => $from_nama,
'phone' => $from_phone,
'status' => '0',
'unique_id' => uniqid()

);

$this->load->model('excel');

$this->excel->tambahuser($data_user);


$this->load->view('kirimundangan.php',$data);
}

模型:

function tambahuser($data_user)
{
$this->db->insert('request', $data_user);
$this->db->insert_id();

foreach ($data_user as $key)
{
$data = array(

'from_name' => $this->input->post('from_nama'),
'from_phone' => $this->input->post('from_phone')
);
}

}

我想我在 success:function(data) 中编写代码时犯了错误,有什么帮助吗?

最佳答案

将 peson 函数更改为

function pesan()
{
var email = $("#email").val(),
from_nama = $("#from_nama").val(),
from_phone = $("#from_phone").val();

$.ajax
({
url : "<?php echo site_url('kirim/undangan')?>/",
type: "POST",
dataType: "JSON",
data:{from_nama: from_nama, email: email, from_phone: from_phone},
success: function(data)
{
$('#alert').show();
$('#email'+data).html(data.email);
$('#from_nama'+data).html(data.from_nama);
$('#from_phone'+data).html(data.from_phone);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error upload data');
}

});
}

并且您期待 json 响应,因此您还必须更改服务器端代码,并且从那里您必须返回带有 json 而不是带有 View 的数据。我希望这对你有用

关于成功函数后javascript返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37538333/

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