What is wrong with this js function that resend activation email - javascript

I have a function to resend the activation email.
When user try to login and user is not activated then a message show up with a link (you can see below).
It doesn't work at all. Can't even reach the js function if i'm correct.
What can be the problem? Please help me out.
Link that call the function:
Aktiváló e-mail újraküldése
JS code:
$(document).on('click', '#resend_activation', function()
{
var email = $(this).attr('email');
$("#success_msgforword").hide();
$(".errors").hide();
$.ajax({
type: "GET",
url: base_url + "aktivalo-ujrakuldes?email="+email,
dataType: "json",
success: function(data)
{
if(data==0)
{
$(".errors").html('Error! You are logged in!').show();
}
else if(data==1)
{
$(".errors").html('');
$(".success_msgforword").html('Success, you wil receive an email from us shortly.').show();
}
}
});
});
Controller that puts the caller link:
if (isset($errors['user_banned'])) { /* banned user */
$this->_show_message('error', $this->lang->line('auth_message_banned').' <br/><br/>Indok: '.$errors['user_banned']);
} elseif (isset($errors['not_activated'])) { /* not activated user */
$test = 'A felhasználói fiók nincs aktiválva! <br/>Aktiváló e-mail újraküldése';
$json = array(
'errors' => $test
);
} else { /* fail */
foreach ($errors as $k => $v){ $test = $this->lang->line($v);}
$json = array(
'errors' => $test
);
}

Related

if use ajax call for contact form submitting, header(location:/path) not working

The issue is if try to ajax call for submitting my contact form. then header("location: /path); starts not working.
if ($response->success) {
$guest = mail($serverMail, $toAdmin, $mailMessageToServer, $headers);
$server = mail($email, $toGuest, $mailMessageToGuest, $headers);
if (($guest) && ($server)) {
// header("location: /contact/thankyou.html");
}
} else {
echo "Invalid captcha! Please enter again. ";
}
And yes, because header redirect is not working. I comment it out. And tried to redirect page inside ajax call like below.
$(document).ready(function() {
var form = $('#mailformpro');
var response = $('.status');
form.on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'contactform/ajax/contact.php',
type: 'POST',
dataType: 'html',
data: form.serialize(),
beforeSend: function(){
response.fadeOut();
response.fadeIn();
response.html('Loading...');
},
success: function(data){
response.html(data).fadeIn();
window.location.href ="/contact/thankyou.html";
},
error: function(e){
console.log(e)
}
});
});
});
But this time, it's only redirecting inside the .status div! like in the image Normally I am displaying a error message in that div...
Hey This is wrong practice if you are using ajax req then php not able to redirect to your destination all you need to redirect in ajax success response simple us
$data = array();
if ($response->success) {
$guest = mail($serverMail, $toAdmin, $mailMessageToServer, $headers);
$server = mail($email, $toGuest, $mailMessageToGuest, $headers);
if (($guest) && ($server)) {
$data['status'] = 1; // for true
$data['message'] = "your success message"; // for true
}
}
else {
$data['status'] = 0; // for false
$data['message'] = "your error message"; // for false
}
//define content type json if you never echo like echo 'ss' then no need to this
header('Content-Type: application/json');
echo json_encode($data);exit;
and get response in ajax request dataType Json to access json array obj
$(document).ready(function() {
var form = $('#mailformpro');
var response = $('.status');
form.on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'contactform/ajax/contact.php',
type: 'POST',
dataType: 'json',
data: form.serialize(),
beforeSend: function() {
response.fadeOut();
response.fadeIn();
response.html('Loading...');
},
success: function(data) {
if (data.status == 1) {
response.html(data.message).fadeIn();
window.location.href = "/contact/thankyou.html";
}
else{
// for error
response.html(data.message).fadeIn();
}
},
error: function(e) {
console.log(e)
}
});
});
});
You have to send header as Base64 string then decode it in the service / Backend.

JQuery Form only submits in certain browser

I'm working on a registration form for my website and it is only working in Firefox (with some issues), but in Chrome it does not work at all.
For example, if I correctly fill out the form in Firefox and submit, the PHP page still loads instead of sending that content to my registration form via my ajax call. However in Chrome, the form doesn't appear to submit at all.
In fact, in Chrome the variable data is set to the $num value in my PHP script, which is then displayed in #modal-body.
Question: How can I get this to work in all browsers and why is my event.preventdefault() not working correctly for Firefox??
Here is my registration form submission code:
$('#register-form').submit(function() {
$('#register-form > .input-group').removeClass('has-error');
$('#register-form .form-required').each(function() {
if ($(this).val().trim() == "") {
empty = true;
$(this).parent().addClass('has-error');
errors = "<strong>You have not filled out the login form correctly.</strong>";
}
else {
$(this).parent().removeClass('has-error');
$(this).parent().addClass('has-success');
}
});
// All fields populated, process the form
if (!empty) {
$('.progress').fadeIn(800);
$('#modal-body').parent().removeClass("has-error");
var formData = {
'username' : $('input[name=usernameReg]').val(),
'email' : $('input[name=email]').val(),
'password' : $('input[name=passwordReg]').val()
};
// Process the form
$.ajax({
type : 'POST',
url : 'db.php',
data : formData,
dataType : 'json',
encode : true,
success : function(data) {
if (data) {
// Fade the resulting message in
setTimeout(function() {
$(".progress").fadeOut(1000);
$("#modal-body").fadeIn(1000);
$("#modal-body").html(data);
}, 1000);
// Fade the resulting message out
setTimeout(function() {
$("#modal-body").fadeOut(2000);
}, 3000);
}
else {
$('#modal-body').addClass('has-error');
$('#modal-body').html(data);
}
}
});
}
// There were empty fields on the form
else {
$('#modal-body').html(errors);
}
event.preventDefault();
});
Here is my PHP code that handles processing of the form and checking to make sure that the account does not already exist, then that it is created in the database:
$hashed = md5($salt.$_POST['passwordReg']);
$email = $_POST['email'];
// Check if account already exists
$username = strtolower($_POST['usernameReg']);
$statement = $conn->prepare($selects['username']);
$statement->bind_param('ss', $hashed, $username);
if ($statement->execute()) {
//$statement->bind_result($results);
$num;
while($statement->fetch()){
$num++;
}
$statement->free_result();
}
else {
trigger_error('Error executing MySQL query: ' . $statement->error);
}
$statement = null;
// If num > 0, account exists.
// printf($num) only present for debugging purposes
printf($num);
if ($num < 1) {
$statement = $conn->prepare("INSERT INTO `User` (username, email, password) VALUES(?,?,?)");
$statement->bind_param('sss', $username, $email, $hashed);
if ($statement->execute()) {
echo json_encode('You have registered successfully');
}
else {
echo json_encode('Sorry, registration failed. Please try again later.');
}
}
else {
echo json_encode('Sorry, registration failed. Please verify that you do not already have an account.');
}
break;
Pass the event into the callback
$('#register-form').submit(function(e) {
// your code...
e.preventDefault();
}

Ajax Jquery - Data Type JSON always response null

I want to create a simple jQuery Ajax code to check user discount code when someone click "Check Discount Code" button. I created this prototype:
<script>
jQuery(function($) {
$("#btn-check-discount").click(function() {
checkdiscountcode();
});
// end document ready
function checkdiscountcode() {
var discountValue = $("#discount_code").val();
var nonce = "<?php echo wp_create_nonce("
getdiscount_nonce "); ?>";
alert(discountValue);
alert(nonce);
$.ajax({
type: "post",
dataType: "json",
url: "<?php echo admin_url() . "
admin - ajax.php " ?>",
data: {
action: "getdiscount",
discountValue: discountValue
},
success: function(response) {
alert(response);
if (response.message == "found") {
alert("Code Correct");
} else {
alert("Code InCorrect!!! Please Try it Again");
}
}
});
}
});
</script>
Functions.php:
add_action("wp_ajax_getdiscount", "getdiscount");
add_action("wp_ajax_nopriv_getdiscount", "getdiscount");
function getdiscount() {
$return = array(
'message' => 'Found',
'ID' => 1
);
wp_send_json($return);
}
This code is not working. I have added some alert(response), hoping I will get Array() response from the alert, but it keep gives me "Null".
Can anyone helps me to find the culprit? I implemented this code in WordPress 3.9.3.
Modify your add_action hooks as like below and give it a try:
add_action("wp_ajax_getdiscount", "getdiscount");
add_action("wp_ajax_nopriv_getdiscount", "getdiscount"); // remove '2' from wp_ajax_nopriv_getdiscount
instead of
add_action("wp_ajax_getdiscount", "getdiscount");
add_action("wp_ajax_nopriv_getdiscount2", "getdiscount");

How to show alert when error comes from the PHP as response to ajax request in following case?

The jQuery AJAX function is as follows :
$(document).ready(function() {
$("#zip_code").keyup(function() {
var el = $(this);
var module_url = $('#module_url').val();
if (el.val().length === 5) {
$.ajax({
url : module_url,
cache: false,
dataType: "json",
type: "GET",
async: false,
data: {
'request_type':'ajax',
'op':'get_city_state',
'zip_code' : el.val()
},
success: function(result, success) {
$("#city").val(result.place_name);
$("#state_code").val(result.state_code);
}
});
}
});
});
The PHP code is as follows :
case "get_city_state":
// to get the city and state on zip code.
$ret = $objUserLogin->GetCityState($request);
if(!$ret) {
$error_msg = $objUserLogin->GetAllErrors();
$data = array();
$data['error_message'] = $error_msg;
$data = json_encode($data);
echo $data;
die;
} else {
$data = array();
$data = $objUserLogin->GetResponse();
echo json_encode($data);
die;
}
break;
Now I'm able to print the success when the response comes without any error but what about showing the alert message when some error happens. How to achieve it? What change needs to make to the above code? Please help me.
Use below condition in success:
success: function(result, success) {
if($.inArray( "error_message", result)) {
alert("Error message");
} else {
$("#city").val(result.place_name);
$("#state_code").val(result.state_code);
}
}
on php file if u found data acc to ur parameter
than it ok if there is no data acc to ur parameter than this time u can call its an error so in thsi case echo "error";
and on ajax page check for result if its value is error then do what else u want
success: function(result, success) {
$("#city").val(result.place_name);
$("#state_code").val(result.state_code);
}

why my ajax something will callback to error

Can somebody can tell me why my code will suddenly callback to error and suddenly can successful randomly?Thanks.
function get_timeframe(){
var v_fldname = "xUPH_exclude_Timeframe";
if ($.trim(v_fldname) != '') {
//alert(v_fldname);
$.ajax({
url:"../ajax/get_timeframe.php",
dataType: "json",
data:{v_fldname: v_fldname},
success: function(data) {
if ( data.result != null ) {
$.each(data.result, function(){
var code_value = this['code_value'];
document.getElementById('v_xUPH_exclude_Timeframe').value = code_value;
//alert(" get v_xUPH_Scan_Count");
});
}
},
error: function(data) {
alert("get_timeframe error");
}
});
}
}
The following php code.
if (isset($_REQUEST['v_fldname']) === true) {
require '../Connections/con_meditop.php';
$query = mysql_query("
SELECT code_mstr.code_value
FROM code_mstr
WHERE code_mstr.code_fldname = '" . mysql_real_escape_string(trim($_REQUEST['v_fldname'])) . "'
");
$result = array();
if(mysql_num_rows($query) == 0)
{
$result = null;
}else{
while ( $row = mysql_fetch_array($query) )
array_push($result, array('code_value' => $row[0]));
echo json_encode(array("result" => $result));
}
}
The issue is on the receiving end of your Ajax call--get_timeframe.php. Press F12 and click the network part of the console, then send the Ajax call. You'll see get_timeframe.php show up in the network console and then turn red. Click it and look at the response body.
Or instead of alert("get_timeframe error"); Do console.log(data); to see a run-down of the error in the console (press F12).

Categories

Resources