I have OOP php file and I would like to call one of the function by AJAX, i have read a lot about this (mostly stackoverflow), but for some reason, it just doesnt work.
I know that ajax function is called (I tried to add some alert into success function and that alert appeared after clicking the button), but somehow it ignores everything from file ajax.php (i even tried to add some echo at the top of ajax.php file, but nothing happened)
also note that I need button, not input (although that would be much easier)
this is my button:
<button class='formular-button' type='button' onclick='prihlasPA()'> vypiš přihlaš </button>
this is my script for button:
<script>
function prihlasPA() {
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(){},
error: function(){
alert("chyba");
}
});
}
</script>
and this is inside ajax.php, which is called by AJAX:
include( 'navstevnik.php' );
echo "aaa";
if(isset($_POST['action']) ) {
$navstevnik = new navstevnik; //navstevnik is name of my class btw
$navstevnik->vypisPrihlas();
}
I dont think it matters, but in my function vypisPrihlas() is this code:
public function vypisPrihlas(){
$this->index=1;
echo '
<fieldset class="formular">
<div class="pure-control-group stred" >
<input id="nick" type="text" name="nickP" placeholder="Nickname">
</div>
<div class="pure-control-group stred">
<input id="password" type="password" name="hesloP" placeholder="Heslo">
</div>
<input class="pure-button pure-button-primary stred" style=width:188px; type="submit" name="buttonP" value="přihlásit" />
</fieldset>';
}
I don't see any issues with the above code.
When the function vypisPrihlas
echos the HTML, you won't see it on your main page. Instead, you need to load it in from your success function and append the HTML somewhere on your page.
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(data){
$( ".your_favorite_div" ).append(data);
},
error: function(){
alert("chyba");
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments