How can I reload a page when using data using ajax success function in javascript codeigniter php?

I have a registration form and you want to check whether the registered user will try to register again to show the warning window already registered using the ajax function.

I used the codeigniter framework.

my function works fine, but when a warning pop-up appears and the ok button is clicked, it reboots. i want to disconnect

my form code:

<form class="modal-content" name="form2" action="<?= $this->config->base_url();?>home/register_user" method="post" onSubmit="return ValidateRegister()"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">Γ—</span> </button> <h4 class="modal-title">Create your account</h4> </div> <div class="page-content vertical-align-middle" style="display:block;"> <div class="form-group has-error"> <label class="control-label" for="inputTokenfieldError-tokenfield"> <?php echo validation_errors(); ?></label> </div> <div class="form-group form-material floating"> <input type="text" class="form-control " id="inputName" name="username" value="<?php echo set_value('username'); ?>" autocomplete="off"> <label class="floating-label">Username</label> </div> <div class="form-group form-material floating"> <input type="email" class="form-control " id="my_email" name="email" value="<?php echo set_value('email'); ?>" autocomplete="off"> <label class="floating-label">Email</label> </div> <div class="form-group form-material floating"> <input type="password" class="form-control " id="inputPassword" name="password" autocomplete="off"> <label class="floating-label">Password</label> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-block">Join Now - It Free</button> </div> </form> 

my javascript function:

 function checkRegistrations() { var email = $('#my_email').val(); $.ajax({ url: "<?= base_url() ?>home/checkRegistration", async: false, type: "POST", data: "email="+email, dataType: "html", success: function(data) { // alert(data); if(data==1) { //event.preventDefault(); alert('Email already registered'); return false; window.location.reload(false); } else{ return true; } } }) } 
+5
source share
2 answers

You return true / false from within the annon. functions, that is, a success handler. But the parent function does not return true / false.

Change your code as follows:

 function checkRegistrations() { var email = $('#my_email').val(); var isValid = true; $.ajax({ url: "<?= base_url() ?>home/checkRegistration", async: false, type: "POST", data: "email="+email, dataType: "html", success: function(data) { if(data==1) { alert('Email already registered'); isValid = false; } } }); return isValid; } 
+1
source

Just add id to the submit button, say #submitbutton .

and use the .prop() jQuery method to set the disabled attribute of the button.

 $("#submitbutton").prop("disabled", true); 

IMP: This will only work if you save the same page to ajax success , but if you reload the page, then you need to check php to see if this form was presented in this current $_SESSION .

So, inside your ajax php handler, you can perform the check as follows.

 session_start(); if(!empty($_POST) && empty($_SESSION['post'])) { $_SESSION['post'] = true; ... do your code unset($_SESSION['post']); }else{ // send the json encoded error message } 

And in the html form, just add a hidden input with the name post and set the value to 1 or something that you consider necessary, so after submitting the form, the post will be installed inside the $_SESSION SuperGlobal Array array, and if the same form is submitted Twice by the same user, php will not accept it.

+3
source

Source: https://habr.com/ru/post/1241608/


All Articles