I’m new with php and I’m facing very difficult on understanding how it works. I work with C# windowsForm and Asp.Net so that’s why i’m having so much difficult.
I have a form:
<html>
<head><title>Numero 1</title></head>
<body>
<form method='post' action="01.php">
Valor da Conta de Luz <input type="text" name="Valor" />
<input type="submit" value="Verificar Valor" onclick="ValorConta()" />
</form>
</body>
</html>
And in the same file i have my php
<?php
function ValorConta()
{
$TxtValor = $_POST["TxtValor"];
if($TxtValor > 50)
{
echo "Você está gastando muito !";
}
else
{
echo "seu gasto é normal !";
}
}
?>
I want to call this method ValorConta by the click of my button, and pass as parameter the value of the textbox… It’s so easy with C# but now I have no idea how to do this… And my teacher is not going so well explaining it to me. Any help ?
Short answer: with an AJAX call to the PHP script, specifying parameters that allow you to execute that certain method. That’s because the PHP script is interpreted before you get the actual page and all the PHP code is executed. Then, JS methods cannot execute any PHP code because, as you can see if you check the source code of the page you get in the browser, there is none left in your page!
Hence, given the fact you’re mixing the function containing some logic, with some presentation output, you have to separate them in two php scripts. Then, the AJAX call with get only the ouput you need.
You must write your new PHP script aiming at receiving parameters that definbe which method to execute and with what data as parameters.