Echo dupa Ajax Request

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
giulian9
Mesaje: 39

Echo dupa Ajax Request

Salutare ,

Cu toate ca am citit documentatia,nu reusesc ca fac un 'echo' dupa ce am trimis datele prin ajax, are cineva idee, de ce nu merge ?
Cod:

Cod: Selectaţi tot

<?php
echo "before if";
// delete part    
    if (isset($_POST['delete']))  {																	
      echo "after if";
			$f_api = false ; //test
      
			if ($f_api) {
        echo " If Statment";
      } else {  
	echo '<script  type="text/javascript"> alert("Else Statment !");</script>'; 
      } 
?>
	<script	  src="https://code.jquery.com/jquery-3.4.1.js"	  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="  crossorigin="anonymous"></script>

	<input type="button" class="btn btn-danger remove"   value="DELETE" style="width:120px !important"><br> 

<script>
jQuery(".remove").click(function() {					
if (confirm("Do you need to delete ?")) {
 jQuery.ajax ({ 
type:'POST',
data: {id:'1234', elementus:'delete'},
success: function(data) {
window.location.href = window.location.href;
} 																		        																					});																				    																																									} });	
 </script>

MarPlo
Salut
Instructiunea "echo" se executa pe server. Cu jQuery.ajax() poti prelua raspunsul in functia de la "success" (in argumentul functiei "data").
Dupa ce e preluat de ajax, raspunsul il poti folosi in javascript.
De exemplu:

Cod: Selectaţi tot

success: function(data) {
alert(data);
window.location.href = window.location.href;
} 
- In codul dat de tine, in functia ajax() nu e parametru "url" care sa contina adresa fisierului de pe server la care se face apelarea.

giulian9
Salut, am reusit sa inteleg si sa rezolv.
Mai jos codul:

Cod: Selectaţi tot

<?php
echo "before if";
// delete part    
    if (isset($_POST['elementus']))  {
	$f_api = false ; //test
      
	if ($f_api) {
        sterge din sql / api 
      } else {  
	exit('Error');
      } 
?>
	<script	  src="jquery-3.4.1.js"	  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="  crossorigin="anonymous"></script>
<input type="button" class="btn btn-danger remove"   value="DELETE" style="width:120px !important"><br> 
<script>
jQuery(".remove").click(function() {					
if (confirm("Do you need to delete ?")) {
 jQuery.ajax ({ 
type:'POST',
data: {id:'1234', elementus:'delete'},
success: function(data) {
if (data = 'Error') { alert('A aparut o eroare'); }
window.location.href = window.location.href;
} 																		        																					});																				    																																									} });	
 </script>

Subiecte similare