Display server error at user end via php

I have a final user page ie index.php that has a button

<button class="rightbtn" type="button" id="submitamt" style="display:none; ">Submit</button>

      

My ajax code:

<script>
function myFunction() {
    alert("You need to login before negotiating! However you can purchase the product without negotiating");
}
var startClock;
var submitamt;
var walkaway;
var digits;

$(function() {
  startClock = $('#startClock').on('click', onStart);
  submitamt = $('#submitamt').on('click', onSubmit);
  walkaway = $('#walkaway').on('click', onWalkAway);
  digits = $('#count span');
  beforeStart();
});

var onStart = function(e) {
  startClock.fadeOut(function() {
    startTimer();
    submitamt.fadeIn(function() {
      submitamt.trigger('click'); // fire click event on submit
    });
    walkaway.fadeIn();
  });
};

var onSubmit = function(e) {
  var txtbox = $('#txt').val();
  var hiddenTxt = $('#hidden').val();
  $.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    success: function(returndata) {

         console.log(returndata);

    },
    error: function() { 
      console.error('Failed to process ajax !');
    }
  });
};

var onWalkAway = function(e) {
  //console.log('onWalkAway ...');
};

var counter;
var timer;
var startTimer = function() {
  counter = 120;
  timer = null;
  timer = setInterval(ticker, 1000);
};

var beforeStart = function() {
  digits.eq(0).text('2');
  digits.eq(2).text('0');
  digits.eq(3).text('0');
};

var ticker = function() {
  counter--;
  var t = (counter / 60) | 0; // it is round off
  digits.eq(0).text(t);
  t = ((counter % 60) / 10) | 0;
  digits.eq(2).text(t);
  t = (counter % 60) % 10;
  digits.eq(3).text(t);
  if (!counter) {
    clearInterval(timer);
    alert('Time out !');
    resetView();
  }
};

var resetView = function() {
  walkaway.fadeOut();
  submitamt.fadeOut(function() {
    beforeStart();
    startClock.fadeIn();
  });
};
</script>

      

on click on that page the backend of the page (page.php) is called via ajax. when this backend script is run I want to add a status check and if it is not fully populated I want to show the error on the front. code on .php page

<?php
session_start();
require 'connection.php';
$sql01 = "SELECT * FROM usertable where email='".$_SESSION['email']."' ";
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0) 
    {
        while($row = mysqli_fetch_assoc($result01)) 
            {
                $userid = $row["id"];
            }
    }

$presentdate = date("Y/m/d");   

$sql00 = "SELECT * FROM userrecord where userid='".$userid."' and datee='".$presentdate."'";
$result00 = mysqli_query($con, $sql00);
if (mysqli_num_rows($result00) > 0) 
    {
        while($row = mysqli_fetch_assoc($result00)) 
            {
                $recordcounter = $row["recordcounter"];
            }
    }

if($recordcounter>=3)
    {
        echo "limit reached"; // wish to display this message on index.php page
    }   
else
    {
       /******run the code********/
    }
?>

      

can someone tell me how can i display a message limit reached

as an alert box in the index.php page when the submit button is clicked

+3


source to share


2 answers


Suggestion: return "{error: true, message: 'limit reached'}"

in case of error with PHP.

After the AJAX call completes, write the response in a variable response

and add validation.

Add the object dataType: 'json'

to your AJAX object. (To parse the response into a Javascript Object).



Modify your success function as follows:

function(response) {
    if(response.error){
       alert(response.message);
    }
}

      

This makes your code readable and someone who is only looking at the client side can figure out what's going on.

0


source


Simple you just need to follow my steps:

Step 1: In Index.php page after submit button create empty div like this

<div id="ajax_reply"></div>

      

Step 2. In your ajax request datatype change datatype

    $.ajax({
       type: do not do any change 
        url: do not do any change
        dataType: "html",
        data: {
          txt: txtbox,
          hidden: hiddenTxt
         }
        // cache: false, remove this line
        success: update this function with my code
        error: do not do any change
    });

      

does not make any changes means it will remain the same as before.



Step 3: In updating your ajax code, you successfully execute your code

    success: function(returndata) {
            $("#ajax_reply").html(returndata);
    }

      

Step 4: update the bottom of your page.php code with these lines:

if ( $recordcounter >= 3 ) { echo "<script type="text/javascript"> alert("limit reached"); </script>"; }else { /****** code ********/ }

I hope this is the answer to your question.

-1


source







All Articles