gpt4 book ai didi

javascript - 如何通过AJAX查看数据库字段

转载 作者:行者123 更新时间:2023-12-03 06:46:31 25 4
gpt4 key购买 nike

我已经创建了一个通过ajax向数据库提交数据的表单。这工作正常。我如何防止输入数据库中已经存在的站点名称。如果该站点名称已经在数据库中,它应该给出错误消息。

Controller

public function user_add()
{

$data_save = array(
"Mnumber" => $this->input->post("Mnumber"),
"email" => $this->input->post("email"),
"fname" => $this->input->post("fname"),
"address" =>$this->input->post("address"),
"sitename" =>$this->input->post("sitename"),
/* "reqnum" => $this->input->post("reqnum"),*/
"title" => $this->input->post("title"),
"descr" => $this->input->post("descr"),
/*"payment" => $this->input->post("payment"),*/
"uniquekey" => $this->input->post("uniquekey")
/*"subscription" => $this->input->post("subscription"),
"email_sent" => $this->input->post("email_sent"),*/
);

if ($this->user_mod->AddUser($data_save)) {
echo "Successfully Saved";
}
else {
echo "error";
}

}

查看

<script>

function save_user_new() {

var Mnumber = $('#Mnumber').val();
var email = $('#email').val();
var fname = $('#fname').val();
var address = $('#address').val();
var sitename = $('#sitename').val();
/*var reqnum = $('#reqnum').val();*/
var title = $('#title').val();
var descr = $('#descr').val();
var uniquekey = $('#uniquekey').val();
/*var subscription = $('#subscription').val();
var email_sent = $('#email_sent').val();
var payment = $('#payment').val();*/

if (sitename != "" && email != "") {
$.ajax({
type: "post",
async: false,
url: "<?php echo site_url('form_con/user_add'); ?>",
data: {
"Mnumber": Mnumber,
"email": email,
"fname": fname,
"address": address,
"sitename": sitename,
/*"reqnum": reqnum,*/
"title": title,
"descr": descr,
"uniquekey": uniquekey
/*"subscription": subscription,
"email_sent": email_sent,
"payment":payment*/
},
dataType: "html",
success: function (data) {
alert(data);
if (data == 'error') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Something wrong.");

} else if (data == 'have') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : This Sitename is already exists.");
} else {
$('#error_msg1').hide();
$('#success_msg').show();
$('#success_msg').html("User details successfully saved.");
/*setTimeout(function() { location="features.php"},2000);*/
location.href = 'freecreate';


}

}

});
} else {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Please enter User Details.");
}
}
</script>


<form action="#" id="form_sample_1">


<div class="form-body">
<div class="col-md-6">

<div class="form-group">
<label class="control-label">Your First Name</label>
<input type="text" class="form-control" id="fname" name="fname" placeholder="Enter text" required="">
</div>


<div class="form-group">
<label class="control-label">Email Address</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address" required="">
</div>
</div>

<div class="form-group">
<label class="control-label">Your Mobile Number</label>
<input type="text" class="form-control" id="Mnumber" name="Mnumber" placeholder="Enter text" required="">
<!--<span class="help-block"> A block of help text. </span>-->
</div>

<div class="form-group">
<label class="control-label">Your Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Enter text" required="">
</div>
</div>

<div class="col-md-6">

<div class="form-group">
<label class="control-label">Your Site Name</label>
<input type="text" class="form-control" id="sitename" name="sitename" placeholder="Enter text" required="">
<span class="help-block"> please enter the sitename only.if you wish to cretae site as john just type john.it will create the site automatically as john.site.mobi </span><br>
</div>

<div class="form-group">
<label class="control-label">Title of Your Web site</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Enter text" required="">
</div>

<div class="form-group">
<label class="control-label">Description of Your Web Site</label>
<input type="text" class="form-control" id="descr" name="descr" placeholder="Enter text" required="">
<!--<input type="hidden" class="form-control" id="req_num" name="req_num" value="1" placeholder="Enter text">-->
<?php $uniquekey = md5(uniqid(rand(), true)); ?>
<input type="hidden" class="form-control" id="uniquekey" name="uniquekey" value="<?php echo $uniquekey ?>" placeholder="Enter text">
<!--<input type="hidden" class="form-control" id="subscription" name="subscription" value="1" placeholder="Enter text">
<input type="hidden" class="form-control" id="email_sent" name="email_sent" value="1" placeholder="Enter text">-->
<!--<input type="hidden" class="form-control" id="payment" name="payment" value="1" placeholder="Enter text">-->

</div>
</div>


<div class="form-actions right">
<!--<a class="btn green" onclick="save_user_new()">Submit</a>-->
<button type="submit" id="save_btn" class="btn green" onclick="save_user_new()">Submit</button>
<button type="button" class="btn default">Cancel</button>

</div>

</div>
</form>

型号

public function AddUser($data_save)
{
if ($this->db->insert('users', $data_save)) {
return true;
} else {
return false;
}

}

最佳答案

编写一个验证函数,它实际上检查文本框中的空值并返回truefalse。考虑下面的例子。

function ValidateForm(form){
var valid=true;
$(form).find('input[type="text"]').each(function(){
if($(this).val()=="")
valid=false;
});
return valid;
}

function save_user_new() {
//getting all the values
//change if condition to
var form=$("#form_sample_1");
if(ValidateForm(form))
{
//continue with ajax
}
else
{
alert('Please fill all the fields');
return false;
}
}

关于javascript - 如何通过AJAX查看数据库字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717456/

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