Run a Function From a Form - php

I want to add a function to a form, so when the submit button is clicked, a function is executed instead of leaving the page.
And I can not use $_POST, because I need the url.
Here is my script :
function submitclicked()
{
echo 'Hello World';
}
<form action="" method="post" id="TransactionAddForm">
<input type="hidden" id="TransactionAccountID" value="'.$zpid.'" name="data[Transaction][account_id]">
<input type="hidden" id="TransactionAmount" value="'.$_POST['price'].'" name="data[Transaction][amount]">
<input type="hidden" id="TransactionDesc" value="'.$desc.'" name="data[Transaction][desc]">
<input type="hidden" id="TransactionRedirectUrl" value="'.$backurl.'" name="data[Transaction][redirect_url]">
<div class="submit"><input type="image" src="http://www.zarinpal.com/img/merchant/merchant-6.png" ></div>
</form>;
Tnx for your help.

Your basic understanding is flawed. PHP is a server side language, meaning, it runs on the server side. It cannot capture button clicks or browser events. It can only work with requests from the client to the server.
Your function may only run after the form was submitted to the server.
For your problem, use JavaScript.
Also, what you have here is not a form. The user has no form controls to choose from here.

Add a return false; at the end of the function you call on submit. This way, the form will not be submitted. You can also add a simple button, which is the type button, and not the type submit. Those buttons will also not submit the form.
-- EDIT --
I am assuming you want to call a JavaScript function on the click of your submit button. any PHP is of course server-side...

Related

Display confirmation message

I need to get confirmation before proceeding.
My code is:
echo "<SCRIPT> confirm('Are you sure you want to continue') </SCRIPT>";
//press ok should keep on processing the code while cancel not.
Currently any button pressed continues.
How can I validate what button in the confirmation pop up window is pressed?
You cannot.
PHP is a server-side language and JavaScript is a slient-side one.
PHP is executed on server long before JavaScript starts to execute in the clients browser.
One way to do that is to split the service in two. You don't even have to use JavaScript confirm, you can just use HTML form with submit button and a hidden value with POST method.
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
After clicking the button the page will be reloaded.
In the PHP script you can check POST variables for that value and if present you can continue doing the comfirmed job using something like this:
<?php
if(isset($_POST['internal_check']) && $_POST['internal_check'] == 42)
{
/* do confimed job here */
}
else
{
?>
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
<?php
}
?>
If the page was not accessed via POST call with the secret variable holding the right value the user will only see the confirmation form. Otherwise the special code will be executed.
If you are not happy with reloading the page the same may be done using AJAX. The basics are the same, but it is a bit more complicated.

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

How do you receive the data when you click the submit button of a form?

After I add a button and a text field, how can I program that button to simply take what's in the text box and put it into a variable? I have no idea how the button click event works.
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="xxxxxxxxx " />
</label>
<label>
<input type="text" name="txtboxsearchbydistro" id="txtboxsearchbydistro" />
</label>
</form>
Would I put a PHP statement in the space where the xxxxxxxx is at?
Any help would be great!
You can't execute PHP code in onclick() statements because PHP gets executed on the server, before the page is sent to the browser, and the onclick() function is exectued at the browser.
Solution would be (assuming this page is form.php) set the action of the form for "form.php" and on that page have
if(isset($_POST)){
$variable = $_POST['txtboxsearchbydistro'];
// Here you can run validation on $variable, sanitize it and pass it to a DB query
}
No, php is server-side, and onClick is client-side event.
I'm not entirely sure what are you trying to accomplish. If you wish to submit your txtboxsearchbydistro value to some PHP script, you would put something like this:
<form id="form1" name="form1" method="post" action="somePhpScript.php">
Then you would use something like Bobby proposed.
If you wish to do something before you actually send the form, or you want to do something on client side (i.e. in visitor's browser), you'd need to do something like
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="myScript();" />
You could then need to define your script, and assign your value there.
Hope it helps.

HTML Javascript forms, and a php script - Simple question, help! what does this code do?

So I have this simple form:
<form action="includes/process.php" method="post" name="standard_use" id="standard_use" enctype="multipart/form-data">
<button onclick="dofunction(); return false;">Do it!</button>
<input type="file" id="upload_file" name="filename" style="float:left;width:70%;" size="42"/>
</form>
So what happens really when the button is clicked ?
Is it that the php file is called ? does it not ? the javascript is called before ?
Anyone can shed some light on this ?
Thanks !
Well, when you hit the button the following events occurs:
You send a REQUEST to the server
The php codes evaluates the request and runs some codes
Finally it returns back a RESPONSE which you see as a web page
Javascript is a client-side script which means that whenever you make an action on the page, the code runs. For instance, when you click the button, before sending the request javascript will work. You may, for instance, place a function that will be triggered when you hit the button which checks the form and either approves the form or shows the error messages
EDIT
As far as your comment is concerned:
Yes, javascript runs first when you hit the submit button. Php runs only when you submit the form and make a request to the server.
Consider this example: (I am better at explaining things with examples:)
<form action="somepage.php" onsubmit="return checkMe()" method="POST">
<input name="firstname" id="fn" value="" type="text" />
<input type="submit" value="Submit" />
<script type="text/javascript">
function checkMe(){
var tb = document.getElementById("fn")
if(tb.value == "Alex") return true;
else return false;
}
</script>
</form>
So basically, when you hit the button and try to submit the form, the javascript will first check whether the name provided in the textbox is Alex or not, if it is not then it will not submit the form. If it is Alex then it will submit the form and then the form will redirect the user to somepage.php. Finally, the php codes will work in somepage.php and the page will be rendered again.
What happens is that only doFunction() javascript function is invoked and nothing more.
However, it might be possible that this javascript function invokes "submit" event on the form and the request is sent (what you described as "php file is called").
Your code just trigger javascript event and your function. To submit a form you need an
<input type="submit" value="Submit" />
or a button, which default type is submit (thx davin)
<button value="Submit" />
However as far as you return false in your javascript code your form won't be submitted even with the submit button.

AJAX: How do I make the button submit after I press "Enter"?

Currently, I am using an with ajax to update my mysql. Now, I have to click on the button with the mouse for it to work (I am using onclick), but how can I make it accept the "enter" button? My guess is... Enter isn't working because isn't there. If I leave it there, my ajax just doesn't move.
Use a <input type="submit> instead of a <button> or type="button" and then hook into the forms' onsubmit event.
For example:
<form action="#" onsubmit="alert('Hello world!'); return false">
<input type="text" name="myText" id="myText" />
<input type="submit" value="Submit" />
</form>
The return false ensures that the browser doesn't actually submit a form.
If you're in an HTML form, then enter will submit the data. You only have to write in the onsubmit event handler to do your ajax calls.
If you're not in an HTML form, check for the key press event handler. Maybe it's what you're looking for.

Categories