Here is my index.php:
<?php
function type_my_text(){
echo filter_input(INPUT_POST, 'textfield')
}
$action = filter_input(INPUT_POST, 'action');
if($action == "typer"){
type_my_text();
}
?>
<html>
<head>
<script src="js/jquery.js"></script>
<script>
function call_typer(){
$.post('index.php', {action : "typer"});
};
</script>
</head>
<body>
<form name="form" method="POST" action="index.php">
<input type="text" name="textfield">
<input type="submit" value="type" onclick="call_typer()">
</form>
</body>
</html>
With this code, I'm trying to call type_my_text PHP function using ajax (post(), in the case) when I click the submit button. I mounted this code based on other answers, but It isn't working and I don't know what I'm missing.
The process:
html button click -> call js call_typer() function -> make jQuery.post() ajax request -> php var $action receive "typer" -> call php function type_my_text()
I'm expecting that this code writes on page what I wrote in the textfield. When I submit the button, nothing happens. I think the ajax request is happening, but filter_input(INPUT_POST, 'action') is receiving nothing or not what I'm expecting ("typer" as value).
No error is being raised.
login()instead oftype_my_text()- where istype_my_text()ever called? Secondly, this is an AJAX call - you didn't show the code to process the return data, or did you not even have it?login()thing. And sorry, I didn't understand your second question.$_POST. And your functioncall_typer()is only sending 1 value, it is missing your 2nd value. Also, it won't do anything since you don't have the 3rd parameter set to a function, on$.post().