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');
}
}
Related
So I try to create a page for category with dropdown, but I can't direct it to the function for each category that I've chosen and load the form view.
This is the view for choosing the category first
<select name="jenisberkas" id="jenisberkas" onchange="this.form.submit()">
<option value="roya">Roya</option>
<option value="peralihan">Peralihan</option>
<option value="validasi">Validasi</option>
</select>
<button type="submit">Submit</button>
And this is my controller look like,
function __construct()
{
parent::__construct();
$this->load->helper('url')
}
public function get_jenisberkas()
{
if(isset($_POST) && !empty($_POST())
{
if($jenisberkas = $this->input->post('jenisberkas')=='roya'; {
$data['jenisberkas'] = $jenisberkas;
$this->load->view('roya_v',$data); //directing to the form view for this category
}
elseif ($jenisberkas = $this->input->post('jenisberkas')=='peralihan';
{
$data['jenisberkas'] = $jenisberkas;
$this->load->view('peralihan_v',$data); //directing to the form view for this category
{
What I want is when an option is clicked and submitted, it will go directly to specific page form that I made for each options, while passing the value to the page.
I know there's something wrong with my controller and I think I don't need a model since this form doesn't involve a database.
Following is the code fixed:
public function get_jenisberkas()
{
if(isset($_POST) && !empty($_POST)) // Removed () from here, added )
{
if($this->input->post('jenisberkas') == 'roya') {
$data['jenisberkas'] = $jenisberkas; // Fix it yourself
$this->load->view('roya_v',$data);
}
elseif ($this->input->post('jenisberkas')=='peralihan')
{
$data['jenisberkas'] = $jenisberkas; // Fix it yourself
$this->load->view('peralihan_v',$data);)
{
I would recommend to use an Intelligent Code Editor like
https://www.eclipse.org/kepler/
UPDATE:
Well, after more than an hour thinking about it, i solved it myself (lmao) with a simple code in my form category view. And after i clicked the submit button, it will go to the specific function made for each category (yay!), that's what i actually want.
so i'm gonna put it here just incase some people out there maybe need solution for this silly case.
this is the view
if (isset($_GET["jenisberkas"])) {
redirect('mulaiberkas/' . $_GET["jenisberkas"] . "");
exit();
}
?>
<form method="get" action="">
<select name="jenisberkas" id="jenisberkas" onchange="this.form.submit()">
<option value="roya">Roya</option>
<option value="peralihan">Peralihan</option>
</select>
<button type="submit">Submit</button>
</form>
and the controller is simply contains function named as the value chosen
function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function roya()
{
$jenisberkas = $this->input->get('jenisberkas');
$data['jenisberkas'] = $jenisberkas;
$this->load->view('roya_v', $data);
}
function peralihan()
{
$jenisberkas = $this->input->get('jenisberkas');
$data['jenisberkas'] = $jenisberkas;
$this->load->view('peralihan_v', $data);
}
}
I have a wordpress website with a ninja form. In the confirmation page I would like to be able to use the values submitted in the ninja form by my users as PHP variables.
Any thoughts on how I should do this?
Let's say I use radiobuttons to ask someone if they are male or female. How do I echo the given value on the confirmation page?
Ideally you should:
1) Create a new custom plugin (this way you will be able to update your Ninja Forms plugin, theme and still have you changes intact and easily transferable to other WordPress site).
2) Register a Ninja Forms Action:
public function register_actions( $actions ) {
$actions['newsuccessmsg'] = new NF_XXX_Actions_NewSuccessMsg();
return $actions;
}
add_filter( 'ninja_forms_register_actions', 'register_actions' );
3) Add an action class:
final class NF_XXX_Actions_NewSuccessMsg extends NF_Abstracts_Action
{
protected $_name = 'newsuccessmsg';
protected $_tags = array();
protected $_timing = 'late';
protected $_priority = 10;
public function __construct()
{
parent::__construct();
}
public function save( $action_settings )
{
}
public function process( $action_settings, $form_id, $data )
{
return $data;
}
}
4) Method process will include action settings, form id and submission data. I personally just dump all details to file to see data format like this:
$f = fopen( ABSPATH . 'log.txt', 'a+' );
fwrite( $f, print_r( $data, true ) . "\n");
fclose($f);
Code snippet above will create a file log.txt in your WordPress root directory.
PS: I would recommend to refer to ninja-forms/includes/Actions/SuccessMessage.php and use it as example.
You can "POST" the form data and access them by using their name attribute value
index.php
<form method = "post" action = "postValues.php">
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="submit" value="Submit"
</form>
postValues.php
<?php
/* Using the name attribute value to access the selected radio button
This should assign either male or female to the variable. */
$selectedGender = $_POST["gender"];
echo "$selectedGender";
?>
Note: The name attribute value for both the radio buttons should be same (It's like grouping together)
So I have these 2 pages
IF the URL of page1 is: http://localhost/project/page1/page1/5
And there's a link in that page that redirects to page2 with the URL:
http://localhost/project/page2/page2/2
(its 3rd segment - 2 is from dropdown value selected)
Is it possible to make the redirected page - page2 with a URL like this:
http://localhost/project/page2/page2/5/2
-so that I could do something with page1's value, like querying dynamically in the database.
VIEW in page1:
<div class="form-group form-inline">
<select id="p2_val">
<?php
foreach($v_p2 as $row) {
?>
<option
value="<?php echo base_url('page2/page2/').$row->p2_val;?>">
<?php echo $row->p2_num; ?>
</option>
<?php } ?>
</select>
<input class="SubmitButton" type="submit"
name="SUBMITBUTTON" value="Submit" />
</div>
Controller in page1:
class Page1 extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('page1_model');
}
public function page1(){
if($this->session->userdata('logged')){
$this->load->model('page1_model');
$data["v_p2"] = $this->page1_model->v_p2($this->session->userdata('user_id'));
$this->load->view('pages/page1', $data);
$this->load->view('templates/footer');
}
}
Controller of page2:
class Page2 extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('page2_model');
}
public function page2(){
if($this->session->userdata('logged')){
$this->load->model('page2_model');
$data["get_p2"] = $this->page2_model->get_p2($this->session->userdata('user_id'));
$this->load->view('pages/page2', $data);
$this->load->view('templates/footer');
}
}
I changed view of page1 into:
<option
value="<?php echo base_url('page2/page2/').$row->p1_val.'/'.$row->p2_val;?>">
<?php echo $row->p2_num; ?>
</option>
No change in the controllers, just worked around with models by using:
$this->uri->segment();
- Most changes were then in the db queries I made.
- Since I added additional segment in page2, I added " ../ " to the paths of the assets.
sample - model for page2:
function v_p2($data){
$p1_val = $this->uri->segment(3);
$p2_val = $this->uri->segment(4);
//DO smth with $p1_val & $p2_val
//These values were used for querying
}
I'm still not sure I'm understanding you but you can do:
public function page2($var1, $var2){
}
http://localhost/project/page2/page2/5/2
where $var1 is 5, and $var2 is 2 in the above url
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.