I have a form in my index.html that looks like this:
<form name="form" method="post" action="send.php">
.
.
.
.
Inside my send.php I have to functions, function generatekey () and function postData(), how call I call the postData() function from my action attribute in my form?
you can also make your action like this:
<form name="form" method="post" action="send.php?postData">
and in your send.php you can do this:
if(isset($_GET['postData'])){
postData();
}
Add a unique hidden field in your form like:
<input type="hidden" name="action" value="postData" />
send.php
<?php
function generatekey () {
// action
}
function postData() {
// action
}
if ( $_POST[ 'action' ] == 'postData' ) {
postData();
}
?>
Or read your submit value, if it's unique.
Basically there is no "easy" way to do that, you need to write some code in your script that will decide what function to call and when, like:
if($_POST['submit'){
postData();
}
Another option is to use one of the many MVC framework out there like Codeigniter or laravel.
Related
I have a page called wc-info.php which is loaded using this function on a page called plugin_admin.php
public function iris_info()
{
include('partials/wc-info.php');
}
public function get_gtin_woo_db($GTIN_Val)) {
// get the gtin number
$key = 'sku';
$getTheMeta = get_post_meta($GTIN_Val, $key, TRUE);
if ($getTheMeta != '') {
$gtinSuccess = 'Yes';
} else {
$gtinSuccess = 'No';
}
return $gtinSuccess;
}
The partial looks like this:
<div class="welcome-panel-column">
<form action="POST">
<input type="text" action="" name="gtin_search">
<input type="submit" action="" name="gtin_submit">
</form>
</div
The problem is how do I pass the POST values to the plugin_admin.php page in wordpress? When submit is clicked, I need the page to be reloaded, and have POST passed to the function.
Try this,
function custom_function() {
if ( isset( $_POST['gtin_search'] ) ) {
// Update to the function which you are going to use
get_gtin_woo_db($POST['gtin_search']);
} // end if
}
add_action( 'init', 'custom_function' );
I hope this will help.
Here i have a form and some php code to run only when submit button is clicked.But the "IF" block run as soon as the page reloads.As if the $_POST array is not empty.I can try with a single element,like !empty($_POST['name']).But i want to test on universal $_POST array.How can i do it?
<?php
require_once 'input.php';
if(Input::exists()){
print_r($_POST);
$arr=array('ball');
echo $arr[0];
}
?>
<form action='' method='POST'>
name:<input type='text' name='name' ></br>
<input type='submit' value='submit'>
</form>
input.php file:
class Input{
public static function exists($type='post'){
switch($type){
case 'post':
return (!empty($_POST))?true:false;
break;
default:
return (!empty($_GET))?true:false;
brek;
}
}
}
Why do you need the Input class? Just do this:
if ($_POST['name']) {
print_r($_POST);
$arr=array('ball');
echo $arr[0];
}
I suggest you add a name to your submit button
<input type='submit' name='submit' value='submit'>
if(isset($_POST['submit']))
{
php code goes here this will only run when submit btn has been clicked
}
If you want a form to submit data to itself omit the action attribute
The input class is incredibly overkill, but I suppose if you want to use it then we can make it work.
Change the Input class to look something like this...
class Input {
public static function exists($type='post') {
if ($type == 'post' && count($_POST) > 0)
return true;
else if ($type == 'get' && count($_GET) > 0)
return true;
}
}
I frankly hate switch statements and this should work just fine. Once again, I think the Input class is overkill.
When a user accesses my page they are taken to a home screen (which currently has a title and 1 button). The one button is start game which should redirect to a view that has the game board on it. How do I send that information from the button to the controller to access a new view.
This is the button that is in my view (the button should send the information to function click() )
<form id="start" action="index.php/click" method="POST" >
<input type="submit" name="start" value="startGame" />
</form>
Now in the controller index.php
function click()
{
$action = $_POST['submit'];
if($action == 'startGame')
{
$this->load->view('welcome_message');
}
}
I have it loading the welcome_message just for sakes to learn how to redirect. (My group hasn't fully build the game board page)
There are couple of things you need to change.
1. Try to use another name instead of "index.php" for controller. For example "test.php". I think index.php is not allowed for controller name.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function click()
{
$action = $this->input->post('start'); // $_POST['start']; also works.
if($action == 'startGame')
{
$this->load->view('welcome_message');
}
}
}
2.Change the value of action as follows in your view.
<form id="start" action="test/click" method="POST" >
<input type="submit" name="start" value="startGame" />
</form>
This should work. Thanks.
try,
function click()
{
$action = $_POST['start'];
if($action == 'startGame')
{
$this->load->view('welcome_message');
}
}
I'm trying to send POST values to the controller and then pass it to the model in PHP but I'm not sure how to go about doing this.
This part of the controller is to see if the user requests for a view like ?action=game. This works.
But I'm trying to modify it to allow $_POST to be sent to it and then to the model.
function __construct()
{
if(isset($_GET['action']) && $_GET['action']!="" )
{
$url_view = str_replace("action/","",$_GET['action']);
if(file_exists("views/" . $url_view . ".php" ))
{
$viewname = $url_view;
$this->get_view($viewname . ".php");
}
else
{
$this->get_view('error.php');
}
}
else
{
$this->get_view('home.php');
}
}
Here's what I got. In the registration form page, the action of the form is ?process=register but it doesn't work.
if(isset($_POST['process']) == 'register)
{
$this->get_view('register.php')
}
Get_view function determines what model to bind with the view
function get_view($view_name)
{
$method_name = str_replace(".php","",$view_name);
if(method_exists($this->model,$method_name))
{
$data = $this->model->$method_name();
} else {
$data = $this->model->no_model();
}
$this->load->view($view_name,$data);
}
Since the action of your form is ?process=register, then process is still in the $_GET superglobal. What you can do to make it use post is add a hidden input field containing process.
With this:
<form method="post" action="script.php?process=register">
The form is POST'ed to script.php?process=register so you have $_GET['process'], not $_POST['process'].
Try this instead:
<form method="post" action="script.php">
<input type="hidden" name="process" action="register" />
To have $_POST['process']. Alternatively, you could keep the "process" in the GET and switch your if statement to check $_GET instead of $_POST.
I have one form with two buttons (add1,add2). So when I click on button add1, I want to call action_add1() and clicking button add2 calls action_add2(). Both functions are part of Controller_Welcome.
How can I achieve this?
class Controller_Welcome extends Controller
{
public function action_add1()
{
//some logic
}
public function action_add2()
{
//some logic
}
}
public function action_form()
{
$action = $this->request->query('action');
if ($action && method_exists($this, 'action_'.$action))
{
$action = 'action_'.$action;
return $this->$action();
}
}
No Javascript required, just send form data to welcome/form with action param as button name.
With JavaScript:
<input type="submit" name="add1" />
<input type="submit" name="add2" />
$('input:submit').click(function() {
$('#myForm').setAttribute('action', 'add_' + this.name);
$('#myForm').submit();
return false;
});
I'm not sure of the exact syntax, but the main idea is here.