my filename is contacts.php that have two submit buttons;i want that if insert button is pressed insert function is called and if select is pressed select is called.i have written following code:
//contacts.php
<?php
if(isset($_REQUEST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
but it is not working .please help
<?php
if (isset($_REQUEST['insert'])) {
insert();
} elseif (isset($_REQUEST['select'])) {
select();
}
Your code is calling insert() even if no button is clicked, which will happen when the page is first displayed.
use post method because it is secure
//contacts.php
<?php
if(isset($_POST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
If you are using return inside function to return the result , you have to use echo to print the result while calling function.
if(isset($_REQUEST['select']))
{
echo select();
}
elseif(isset($_REQUEST['insert']))
{
echo insert();
}
As has been described by several people (summarizing the previous comments), you have two options.
The first is to send the data via POST or GET to the server directly and reserve (refresh) the page based on whatever you do inside select() and insert().
While this is not the right place for a POST v GET discussion, convention is to use POST when sending data to the server. POST is slightly more secure because the information is not stored in the browser. Read more about the two here: http://www.w3schools.com/tags/ref_httpmethods.asp
The second option is to use AJAX to accomplish your task without refreshing the web page. In short, AJAX uses Javascript methods that you place on your page to communicate with your server, thus avoiding the need for the PHP on the server to actually change anything on the page (which would require a refresh). A code example of AJAX can be found here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
<?php
$insert = $_POST['insert'];
$select = $_POST['select'];
if ($insert) {
insert();
}
if ($select) {
select();
}
else {
echo 'press any button...';
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select() {
echo 'you pressed the [select] button';
exit;
}
function insert() {
echo 'you pressed the [insert] button';
exit;
}
?>
Related
After a button is clicked, the page refreshes, which interferes with further functions of the page.
I tried changing the type from "submit" to "button", but then the button doesn't work at all. I also tried to return false the functions.
My simple code goes like this
<html>
<body>
<form method="Get" action="">
<input type="submit" name="buttonV" onclick="function1()"> <br>
<input type="submit" name="buttonS" onclick="funciton2()">
</form>
</body>
</html>
<?php
if ($_GET) {
if (isset($_GET['buttonV'])) {
function1();
} elseif (isset($_GET['buttonS'])) {
function2();
}
}
function function1()
{
// do stuff
}
function function2()
{
// do stuff
}
?>
What should I do in order to prevent the page to refresh?
I saw you are using php. I don't you use javascript.
You can use 'event.preventDefault()'.
I'm having a rather irritating issue when trying to submit data from a html button tag via POST to a PHP processing script.
Here's my code...
(1) HTML form:
<form id="pub-form1" method="post">
<button type="submit" name="All" value="true">All Publishers</button>
<button type="submit" name="Current" value="true">Current Publishers</button>
<button type="submit" name="Users" value="true">User Priveleges</button>
</form>
(2) jQuery script to handle POST request:
$(document).ready(function () {
$('#pub-form1').submit(function (e) {
e.preventDefault();
$('#results').contents().remove();
var formData = $(this).serialize();
$("input").prop("disabled", true);
request = $.post('VRC_PublishersProcess.php', formData, resultsMessage);
request.fail(function() {
$('#results').append("<span id=\"reply\">Your search failed for an unknown reason. Please try again in a few minutes.</ span>");
$("input").prop("disabled", false); });
function resultsMessage(data) {
$('#results').append(data);
$("input").prop("disabled", false);
}
});
});
(3) PHP processing script:
//All Publishers
if(isset($_POST['All'])) {
if(!empty($_POST['All'])) {
$result = $members->selectAllMembers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
//Current Publishers
if(isset($_POST['Current'])) {
if(!empty($_POST['Current'])) {
$result = $members->selectCurrentPublishers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
//Users
if(isset($_POST['Users'])) {
if(!empty($_POST['Users'])) {
$result = $members->selectUserPublishers();
if($result === true) {
exit(); //Kill the process
}
else {
echo $result;
exit(); //Kill the process
}
}
else {
$error = "<span id=\"reply\">Not all fields were entered correctly!</ span>";
echo $error;
exit(); //Kill the process
}
}
I'm not quite sure what the cause of the issue is. No errors were thrown by javascript or PHP; the page remains blank with no results.
The jQuery function seems to be working correctly. I've use that particular several times on other forms and it handles the POST request properly. I was even able to trigger a failure on it; it responded correctly.
The PHP processing script also appears to be functioning properly. Again, it derived from other scripts that have successfully processed POST requests from the same page.
My suspicion is focused on the HTML form itself. These are my theories: (1) The button isn't submitting "itself." In other words, since there is no text or other forms of input, there's really nothing to submit. (2) For some reason the name is not corresponding to what my PHP script is looking for. For instance, if "All Publishers" was submitted, my PHP script is looking for $_POST['All']. But for some reason, that request isn't saved under that identifier in the PHP superglobal array.
My goal is to provide the end user with three options of displaying data. Right now, that isn't working.
Any feedback is appreciated.
Final Solution:
<div id="pub1">
Other
<div id="displayselect" method="post">
<form id="select1" method="post">
<input type="hidden" name="All" value="1" />
</form>
<form id="select2" method="post">
<input type="hidden" name="Current" value="1" />
</form>
<form id="select3" method="post">
<input type="hidden" name="Users" value="1" />
</form>
<button type="submit" name="All" form="select1">All Publishers</button>
<button type="submit" name="Current" form="select2">Current Publishers</button>
<button type="submit" name="Users" form="select3">User Priveleges</button>
</div>
</div>
Obviously, appropriate modifications were made in the jQuery...
Figured it out,
jQuery.serialize didn't work out on the button (http://api.jquery.com/serialize/) it only works on input, textarea, select so I tried switching your HTML button elements into <input type="submit" ... but that didn't work out as well because jQuery ignores the submit tags when serializing, I tried to add an <input type="hidden" name="test" value="1" /> and checked out what it serialized was test=1 so you'd have to change your form a bit.
jsFiddle for testing: http://jsfiddle.net/4D8Nz/
I tried to build a multi-language site, but the problem the variable can't be set after clicking the submit button for choosing language:
<form action="<?php $aradown->make_lang(); ?>" method="post">
<input type="submit" name="en" value="english" >
<input type="submit" name="ar" value="arabic" >
</form>
Class function code:
public function make_lang(){
if($_POST['en']){
$_SESSION['lang_en'];
}
if($_POST['ar']){
$_SESSION['lang_ar'];
}
}
public function check_lang(){
if(isset($_SESSION['lang_en'])){
$lang="english";
}
if(isset($_SESSION['lang_ar'])){
$lang="arabic";
}
$path=dirname(__FILE__)."/languages/".$lang.".php";
return $path;
}
And this is the code to use:
include('includes/core.class.php');
$aradown= new aradown;
$lang_file=$aradown->check_lang();
include($lang_file);
I tried to print the result of $lang_file, but the $lang var is empty.
C:\AppServ\www\aradown-new\includes/languages/.php
Any thing missing?
You need to actually set the variables to something.
if($_POST['en']){
$_SESSION['lang_en'] = true;
}
if($_POST['ar']){
$_SESSION['lang_ar'] = true;
}
As well as start the session using session_start().
I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
</body>
</html>
In the following line
<form method="post" action="display()">
the action should be the name of your script and you should call the function, Something like this
<form method="post" action="yourFileName.php">
<input type="text" name="studentname">
<input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>
<?php
function display()
{
echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
display();
}
?>
you don't need this code
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
Instead, you can check whether the form is submitted by checking the post variables using isset.
here goes the code
if(isset($_POST)){
echo "hello ".$_POST['studentname'];
}
click here for the php manual for isset
Assuming that your script is named x.php, try this
<?php
function display($s) {
echo $s;
}
?>
<html>
<body>
<form method="post" action="x.php">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
PHP is run on a server, Your browser is a client. Once the server sends all the info to the client, nothing can be done on the server until another request is made.
To make another request without refreshing the page you are going to have to look into ajax. Look into jQuery as it makes ajax requests easy
If you want to call a function on clicking of submit button then you have
to use ajax or jquery,if you want to call your php function after submission of form
you can do that as :
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
Write this code
<?php
if(isset($_POST['submit'])){
echo 'Hello World';
}
?>
<html>
<body>
<form method="post">
<input type="text" name="studentname">
<input type="submit" name="submit" value="click">
</form>
</body>
</html>
An alternative, and perhaps a not so good procedural coding one, is to send the "function name" to a script that then executes the function. For instance, with a login form, there is typically the login, forgotusername, forgotpassword, signin activities that are presented on the form as buttons or anchors. All of these can be directed to/as, say,
weblogin.php?function=login
weblogin.php?function=forgotusername
weblogin.php?function=forgotpassword
weblogin.php?function=signin
And then a switch statement on the receiving page does any prep work and then dispatches or runs the (next) specified function.
I want to build an string/text manipulation app that:
An app consists of a form
User inputs text there as a string using
Click a button - which usues $_POST method to manipulate a string using oop method to :
Example method assigned to a button:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}
Then the manipulated string is displayed in the same form.
User can do another manipulation with a converted string
How to assign method to a button to trigger the function and display results in the same form?
Can it be achived in a single php file to send and get a result?
(I want to store my classes in a seperate file)
Any help/idea Appreciated - any wise guy?
edit:
<?php
require_once('mod.php');
$string='';
if (isset($_POST['string']))
$string=$_POST['string'];
if (isset($_POST['DoStuff']))
{
//$string = reverse_1_word($string);
**$string->reverse();**<-------------------------------Fatal error:---------
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?
I would do something like that:
<?php
Class MyString{
private $string='';
function __construct(){
if (isset($_POST['string']))
$this->string=$_POST['string'];
}
function doStuffWithThisString(){
$this->string=$this->string.'!';
}
function doOtherStuffWithThisString(){
$this->string=$this->string.'!!!';
}
function getString(){
return $this->string;
}
}
$myString = new MyString();
if (isset($_POST['DoStuff']))
{
$myString->doStuffWithThisString();
}
if (isset($_POST['DoOtherStuff']))
{
$myString->doOtherStuffWithThisString();
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?> is the same as <?php echo $string->getString(); ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
You could have a form like this :
<form action="currentFile.php" method="post">
<input type="text" value="myValue" name="text" />
<input type="submit" value="Reverse !" name="reverse" />
<input type="submit" value="Other manipulation" name="otherManip" />
</form>
When you receive the data, you can do this :
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}
to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>
To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}