I want to so when a button is pressed on my page it runs a query in the database
View
<form method="POST" action="evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
Controller
public function query()
{
// load the index view
$this->load->view('querycheck');
}
querycheck.php
<?php
if(isset($_POST['nw_update']))
{
echo("You clicked button one!");
//and then execute a sql query here
}
else
{
echo" dhur";
}
An Error Was Encountered
The action you have requested is not allowed.
I think problem with your action attr of form try like
<form method="POST" action="<?php echo base_url(); ?>/evcccontroller/query">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
add base url to form action
action="<?php echo base_url(); ?>/evcccontroller/query"
Just try
<form method="POST" action="<?php echo site_url('evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
code is looking fine. I think the form action url is wrong. try this:
<form method="POST" action="<?=base_url('index.php/evcccontroller/query')?>">
<input type="submit" name="nw_update" value="NW_Update"/>
</form>
Related
I have two PHP files: index.php with a form, and data.php for further data manipulation.
Here is index.php:
<?php
session_start();
require_once("../index.conf");
$language = new Language();
$lang = $language->getLanguage(#$_POST['lang']);
?>
...
<form name="myForm" action="data.php" method="post" onsubmit="return validateForm()">
<input type="text" name="title" placeholder="e.g.: my_title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>">
...
<button class="btn_r" name="submit" type="submit">
<?php echo $lang['submit-button']; ?>
</button>
Here is data.php:
// success message
echo sprintf('
<div class="success">Good job! Your file <em>'.$file.'</em> was successfully created with this HTML content:<br>
<form name="goto_preview" method="post">
<input type="hidden" name="img_title" value="'.$title.'">
<button class="btn_l" name="reset" type="submit" name="logout" formaction="preview.php">PREVIEW RESULTS</button>
<button class="btn_r" name="submit" type="submit" name="continue" formaction="index.php">CORRECT DATA</button>
</form>
</div>',$img_name);
I try to return the user to the form, with the original values filled in if correction is needed. But the form always opens empty. What is wrong with my code?
Nothing is wrong with your code. That's just the way php forms work, you're redirecting to a new page therefore the form isn't filled out. To change that you could pass the POST arguments that you receive in the data.php and pass them back to index.php where you set them as default values (if present)
I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question
I have a problem, i want to print a a div after form validating and then submitting. Issue is, when call print.window function in submit button, it display print window without validating form fields.
input type="submit" value="Submit" class="button" name="submit" onclick="window.print()">
while the div which to be print is in
if($_POST['submit'])
{
echo"print div here";
}
Please help.
Thanks
If you want to print something after form validating and then submitting, you are supposed to do it in client side.
You can do something like this:
<form name="myForm" action="action.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Then, validate form like this and print.
<script>
function validateForm() {
// validate your form here
window.print();
}
</script>
Hope it helps.
I have this very basic form in my html page.
<form action="post.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and then stores the data onto my database backend.
id also want to submit data via URL bar, such as this.
http://localhost/test.php?message=test&submit=send
but when i try to do above, nothing happens.
how can i achieve such method?
[EDIT]
my post.php
<?php
include_once("connect.php");
if (isset($_GET['submit'])) {
if ($_GET['message'] == "") {
echo " no input, return";
exit();
}
else {
$message = $_GET['message'];
mysql_query("insert into data (message) values ('$message')");
header ('location:index.php');
exit ();
}
}
else {
echo "invalid";
}
?>
use GET method instead of POST
so your code should be like follow:
<form action="post.php" method="GET">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and in the post.php you can get those Query string by using $_GET['message'] or $_REQUEST['message']
use a form GET method. to submit data of a form as a query string.
<form action="test.php" method="GET">
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.