I dont know much about PHP functions.I want to print the name inside the function when it is called on clicking the button.I have tried different ways and even searched for it in google.Need help...
<?PHP
function writeName()
{
echo "Kai Jim Refsnes";
}
Echo "<form>
<input type='button' onClick='writeName();' style='position:fixed;bottom:0px;left:0px;width:100%;'>
</form>";
?>
You can't call a PHP function from JavaScript.
PHP is interpreted on the server side and JavaScript on the client side.
You could use an AJAX request or you could write a JavaScript function at runtime with the PHP
Something like:
echo <<<END
<form>
<input type='button' onClick='function writeName() { document.write("{$php_var_name}") }' style='position:fixed;bottom:0px;left:0px;width:100%;'>
</form>
END
I can provide you with three alternatives:
1. Using pure PHP.PHP is server-side Changes in pure PHP takes place in the following way
Data is sent to the server
Data is processed at the server
After processing data is responded from the server to the user
During the above processes page reloads and when complete data is received at the client reload stops.
So using pure PHP i can figure out only one way to call function on button click.I can be done as follows:
<?PHP
function writeName()
{
echo "Kai Jim Refsnes";
}
if(isset($_GET['submit']))
{
writeName();
}
?>
<form action="#">
<input type="submit" name="submit" value="submit" style="position:fixed;bottom:0px;left:0px;width:100%;" />
</form>
2. Using Javascript.Javascript is client-side.It doesnt reload the page.
<script>
function writeName()
{
document.write="Kai Jim Refsnes";
}
</script>
<button onclick="writeName();">submit</button>
3. Using AJAX.With AJAX you can combine both PHP and Javascript.For this simply print a message AJAX will be too much but for larger/complex data transfers between server and client without reloading AJAX is much helpful.
AJAX refrences: http://api.jquery.com/jQuery.ajax/ ; http://www.w3schools.com/ajax/
PHP is run on the server side, so you should use AJAX, which allows you to run PHP after a page has already loaded.
AJAX is asynchronous javascript - you basically use Javascript to query a PHP page on your server, and you receive a response that you then can print to your page.
Related
Good day,
Although I'm an old programmer, I'm new to php, so I need help.
Context
1. I have a submit form with an update button.
2. I have php code to insert records in a MySQL database.
Problem
1. When I first start the page, the php code runs.
2. When I refresh the page, the php code runs again.
Code
<!-- Button definition -->
<input id="submit" type="submit" class="button" name="submit" value="submit" onclick="Update_MySQL()" />
<!-- Function to be executed by the onclick -->
<script>
<?php
function Update_MySQL() {
if(isset($_POST['submit']))
{
$sql = "MYSQL UPDATE STRING" ;
$MY_CONNECTION->query($sql) ;
}
}
?>
</script>
What I know
1. PHP runs on server and javascript on the browser (client)
2. PHP delivers data to the server when the page is launched or refreshed
Can anyone help me on this?
Many Thanks
You can't prevent this when using the native html <form action="form.php"> way, but there is a way around. After progressing your form, you can redirect using header() to another page (or even the same) with a GET-Redirection-Code (see wikipedia).
if (isset($_POST['field'])) {
// progress data
header('HTTP/1.1 303 See Other');
header('Location: '.$_SERVER['PHP_SELF']);
exit;
}
Another completely different but also working approach is to use javascript to submit your form data.
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.
If your PHP file containing the function ValorConta() is called 01.php and located in the same directory as your HTML file, this should just work.
Be aware however, that PHP is executed on the server, not on the client. So you can't use onclick to call a PHP function. The only thing that can trigger a PHP function is a new HTTP request to a PHP script calling that function. This can for example be achieved by sending a form via POST as you do in your HTML file.
So hello everyone I have a big problem right now with a button that is made depending on the variables that are sent to the page in this case it's a button the when it's clicked it will access a DB and extract everything in the DB and put it into excel format. The problem right now is when the button is made it always execute the DB search and file making. I know it has to do it because of the way the button is made right now, is there anyway of changing this?
if ($action_db) echo "<button class=\"btn\" onClick='".getUDB()."'>" . DBUser . "</button>";
to this:
if ($action_db) echo "<button class=\"btn\" onClick=\"getUDB()\">" . DBUser . "</button>";
and sending the event trough java-script so it executes the getUDB() method that's on my PHP file?
Thanks.
OK so I went and used part of the suggestion that Sam said and put it on a JQuery and the change is like this
if ($action_db) echo "<button class=\"btn\" id=\"UDB\">" . DBUser . "</button>";
JQuery:
<script type="text/javascript">
$(document).ready(function(){
$("#UDB").click(function(){
<?getUDB();?>
});
});
OK so the problem is that when I select the clients menu or any other part of the menu to access that part of the webpage it's automatically doing the getUDB method and won't let me access anything else. Any suggestions???
If you don't want the PHP function getUDB() to be called when you load the page, then you're right - you want to move this out into a Javascript function.
By doing echo "foo" . getUDB() . "bar", I'm assuming your page is loading slowly. To solve this, you can use Javascript with an AJAX call. So you probably want to:
Create a separate PHP file that calls getUDB().
Create a Javascript function on your original page that issues a GET request to this new, separate PHP file. It's easiest to do this using jQuery, which makes AJAX requests nice and easy - check out this page.
Write the Javascript needed to handle the response from that page appropriately.
SitePoint has a good tutorial on jQuery and AJAX, check it out.
You need to send a request to the server to initiate anything server-side in response to an event, either by a traditional post-back or via AJAX.
I suggest you make your buttons into forms, substituting the ID of the user for the value of the hidden input:
if ($action_db) {
?>
<form action="/getUDB">
<input type="hidden" name="DBUser" value="<?= DBUser ?>" />
<button class=\"btn\" type="submit"><?= DBUser ?></button>
</form>
<?
}
And then set up a second script to handle POST requests to /getUDB
desired sequence:
SaveButton
LoadButton
When the "load button" is pressed, load a textfile from the server into a textbox.
Allow the user to add text
When the "save button" is pressed, save the textbox text to the server.
I am having issues with callbacks not executing.
I am worried about the callbacks, I know how to save a file.
<html>
<body>
<button id="1" onClick="reply_click(id)">Load</button>
<button id="2" onClick="reply_click(id)">Save</button>
<?php
function reply_click($clicked_id)
{
echo "hello ";
}
?>
</body>
</html>
You're mixing JavaScript and PHP.
PHP is figured out and executed by the server. Then the web page is sent to the client. Then the user can interact with the page using JavaScript.
You'll have to change the onClick events to use AJAX that send a request to a PHP page.
You could use jQuery to handle AJAX requests without having to mess with low-level things like XMLHttpRequest.
from my this code
<script type="text/javascript">
function getValue()
{
console.log("<?php echo $_POST['input_value'] ; ?>") ;
return false ;
}
</script>
<form id="request_form" action="" name="request_form" method="POST" >
<input id="input_value" type="text" size="45" name="input_value"></br>
<button class="button" id="btForm" onclick="return getValue()" >submit</button>
</form>
I expect it will print some value that I put but it show nothing.
What am I wrong?
Your form isn't actually submitting to a php page. It's just calling getValue, which won't have anything in it yet.
There are two halves to what you are trying to do, and your mistake is that you have mixed them together. The first is the server side, with the PHP. When you submit a form, you should use:
<input type=submit value="Submit" />
This refreshes the page (because the form action is blank), which, using PHP, can read your POST value and print it back out along with the rest of the page. Once the page is finished loading, PHP is no longer involved and cannot affect the behavior.
As you have it right now, when you load your page it looks like this:
<script type="text/javascript">
function getValue()
{
console.log("") ;
return false ;
}
</script>
Since the POST value wasn't there the first time the page loaded, the javascript sent to the browser looks like that - there is no value to print. Clicking the button will execute getValue() as it appears here.
PHP is server side, and is done before the page is loaded in the browser. When first loaded there is no $_POST at the time, so the log is empty.
When submitting the form, you are calling the console.log function which has no text in it, and then returning FALSE, so the form never actually gets submitted.
You need to either set the action attribute in the form tag to point to a PHP page to process the $_POST data, or you need to use AJAX to post the form.
function getValue() {
console.log(document.request_form.input_value.value) ;
return false ;
}
could have a sense, at the moment whe you execute getValue () in JS (client-side) - PHP (server-side) is not involved so no way to echo $_POST....
You can't write out actual server-side code from javascript, and expect it to execute client-side.