Home > Software engineering >  show alert php to axios
show alert php to axios

Time:11-19

I want to alert something in php to axios but it doesn't work.

axios.post(url, formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(function(res) {
  alert("Successfully enrolled.\nPress ok to go back");

})
.catch(function(error) {
  alert("error");
})

php code

if($bal>=2500) {

} else {
  echo '<script language="javascript">';
  echo 'alert("insufficient balance")';
  echo '</script>';
}

I want it to alert if $bal is less than 2500 but the alert wont appear.

CodePudding user response:

Here's a possible way to make this work. Your Javascript could look like this:

axios.post(url, formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(function(res) {
  if(res.data.success === true){
    alert("Successfully enrolled.\nPress ok to go back");  
  }else{
    alert(res.data.message);
  }
)
.catch(function(error) {
  alert("error");
})

PHP

$response = array();
if($bal>=2500) {
  $response['success'] = true;
  $response['message'] = 'All ok';
} else {
  $response['success'] = false;
  $response['message'] = 'insufficient balance';
}
//this assumes that this is the only thing returned from PHP when making the ajax call.
echo json_encode($response);

CodePudding user response:

Use JavaScript , this is meant to be in client side.

  • Related