I need to send an action and a variable to the url - php

This is the action in my form
action="/Classes/Controllers/DoctorController.php?action=editVC"
the action I am sending is ?action=editVC
I need to also send a variable I got from this block of code
<?php
if(isset($_GET['userID'])){
$currentUserID = $_GET['userID'];
}
?>
Given that they are all in the same file, and I want to send the variable $surrentUserID like this ?userID=$currentUserID in the url as well along with the action.
I have tried this way but it did not work
action="/Classes/Controllers/DoctorController.php?action=editVC&userID=$currentUserID"

It looks like you just need to invoke PHP and print the variable, so either in your HTML:
<form action="/Classes/Controllers/DoctorController.php?action=editVC&userID=<?php print $currentUserId; ?>">
Or you might prefer to concatenate the action in a string first like:
PHP:
<?php
$action = '/controllers/DoctorController.php?action=editVC&userID='.$currentUserId';
?>
HTML:
<form action="<?php print $action; ?>">...</form>

Related

I can only pass my $_GET variable to the header() method after sumitting the form (POST method)

Good morning, afternoon or night :D
I want to pass my $_GET['area'] variable to a method, but after submitting the form, it only let me pass such variable to the header() method:
Note: This is the variable from the URL I want to pass: /index.php?area=work
I have a index.view.php with a button:
<a href="crearArt.php?area=<?php echo $_GET['area'] ?>" >Add new article</a>
From the URL, the button receives the $_GET['area'] and when I click the button it takes me to the area where I can create new articles, here is the code of crearArt.view.php file:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
//... Bunch of inputs
<button type="submit" name="crearArticle">Create New Content</button>
</form>
When I click on "Create New Content" (submitting the form), the crearArt.php file should get the $_GET['area'] variable and pass it to the $addDataToDB() method but that doesn't happens, if I put a print_r() inside the if condition to check if the variable it doesn't show the variable.
Furthermore, if I pass the variable to the header() method it does work, meaning that (I think) the variable still there.
Here the crearArt.php file
<?php
$inputNames = [];
$inputValues = [];
if(isset($_POST['crearArticle'])){
// Get all inputs from the form
foreach($_POST as $key => $value){
if($key !== 'crearArticle'){
// This will NOT add the button' name property when the form gets submitted
array_push($inputNames, $key);
array_push($inputValues, $value);
};
};
// Add values to WorkDB || CodeDB
$addDataToDB($inputNames, $inputValues, $_GET['area']);
// Return to index.php?area=??
header('Location: index.php?area='.$_GET['area']);
} // END MAIN IF
?>
Sumerizing, when I press on "Create New Content" it should pass the variable to $addDataToDB() but it doesn't do it, even though the URL has the $_GET['area'] var with it: /crearArt.php?area=work
Question:
Is there a way to get this variable without creating hidden inputs in the crearArt.view.php?
Thanks in advance
PS: I'm new into PHP.
In addition
I tried creating a var outside the if conditional and then passing it to the method but that doesn't work either.
By using that variable I'm gonna let the function "know" from which area I come from so the function "know" to which database it has to refer to.
If I use variable as a string ('work'), it does work.
Each HTTP request you make has its own URL.
The request you make to crearArt.php?area=<?php echo $_GET['area'] ?> and the request you make to <?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?> are different requests with different query strings and will have different values in $_GET.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>"
According to the manual, PHP_SELF is:
The filename of the currently executing script, relative to the document root.
You haven't put a query string in your URL.
When you submit the form $_GET['area'] won't exist.

HTML form: action clarification

Hello I am currently making a registration page,
I'm new to php, and was wondering if this would work when I use the action part in form
<form method="post" id="formArea" action="action.php">
action.php is a file that contains
redirect.html
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
would this work? or is there a way where I can have both in the action section without the need for having to make a new file?
You Could Just Do This From The Form. If This is what your looking for:
<form method="post" id="formArea" action="<?PHP $_SERVER['PHP_SELF'];?>">
//this will make the form post to same page as the form
if your wanting to send this to both you can save the $_POST Data In A Variable and Use It Later.
EX:
<?PHP
//your form submit code might look something like this same file as your form
if(isset($_POST['submit']))
{
foreach($_POST as $key -> $value)
{
//echo or show your values or store your values in database or w/e
$_SESSION['post'][$key] = $value;
//this will make a session variable to be used again later each session array is marked with post identified by $key and stored a $value
}
//you can call your other page here this will only run after this page runs and loads your information above and then redirect with a session variable.
header("Location: redirect.html");
}
else
{
echo "<form method=\"post\" id=\"formArea\" action=\"".$_SERVER['PHP_SELF']."\">";
}
This Code Is Not Tested But It Would Work the redirect.html will process code from $_SESSION['post']

Action of forum doesn't get ID

I have this set in top of page
if (isset($_GET["edit"]) and !empty($_GET["edit"])) {
$edit_id=(int)$_GET["edit"];
$edit_id=sanitize($edit_id);
}
and then i do this with my action on the topbar it shows the number of id
but if i do ""view source" i see this:
<form class="form" action="categories.php<?=((isset($_GET['edit']))?'?edit=.$edit_id':'');?>" method="post">
Why is it not getting the id?
because you are setting it as a literal string. try
action="categories.php<?php echo (isset($_GET['edit'])) ? '?edit='.$edit_id :'');?>"
There are a couple issues going on here:
You are trying to use variables inside single quotes
You are using string concatenation from within a string, which is effectively a period
Try this:
<?php
// We can check this way since you sanitize it at the top
$param = is_numeric($edit_id) ? 'edit='.$edit_id : '';
?>
<form
class="form"
action="categories.php<?php echo($param); ?>"
method="post">

Including form data in another page

When I post my form data:
<form action="layouts.php" method="post">
<input type="text" name="bgcolor">
<input type="submit" value="Submit" align="middle">
</form>
to a php page, "layouts.php", it returns the string, as expected.:
$bgcolor = $_POST['bgcolor'];
echo $bgcolor; //returns "red"
echo gettype($bgcolor); // returns "string"
But when I include "layouts.php" in another page it returns NULL.
<?php
include("php/layouts.php");
echo $bgcolor; //
echo gettype($bgcolor); //returns "NULL"
?>
How do I pass the variable to another page?
You'll have to use a session to have variables float around in between files like that.
It's quite simple to setup. In the beginning of each PHP file, you add this code to begin a session:
session_start();
You can store variables inside of a session like this:
$_SESSION['foo'] = 'bar';
And you can reference them across pages (make sure you run session_start() on all of the pages which will use sessions).
layouts.php
<?php
session_start();
$bgcolor = $_POST['bgcolor'];
$_SESSION['bgcolor'] = $bgcolor;
?>
new.php
<?php
session_start();
echo $_SESSION['bgcolor'];
?>
Give the form two action="" and see what happens. I just tried that on a script of mine and it worked fine.
A more proper way to solve this might exist, but that is an option.

PHP in PHP, Problem with dynamic forms

I'm a newbee
I've got a little problem with my php-script. I try to generate dynamic formulars with php. which works very good. the problem is, I want my data to be send to another funcion in another php-script. I gues its a problem with the syntax:
<?php .... <form action=\"<?php myfunction() ?>\" ... > ... ?>
When I used it in a normal html site, it works fine:
<html> .... <form action="<?php myfunction() ?>" ... > ... </html>
So I'm lokking for a way to bring it in my php-script.
when I try this:
<?php .... <form action=\"" . myfunction() . "\" ... > ... ?>
the value won't be given to my 2nd php script
hope u have an idea, thank u guys, mirrow
If you want to pass form data to function in another script, you must just call that script from the form:
<form action="secondscript.php">
And find a way to call your function, like:
secondscript.php:
...
if ( isset( $_GET['somefield'] ) ) myfunction();
function myfunction()
{
// do something with form data
}
...
I believe, what you want is impossible! You can't send form data directly to a php function.
You need something like this:
<form action="receiving.php" method="post"><!-- or method="get" -->
<!-- your form code -->
</form>
Than you need the php of the name in the action attribute:
<?php
function your_form_processing_function($_POST) { // or $_GET
// process data
}
?>
a form's action determines the URL to which the form's data is sent to.
if you want to send your data to another page, the action field should be a path to that php page. if you want it to go to the same page, you can add a hidden field describing what function to go to, and send it to a page that routes the form to the given function based on that field.
Use this format:
<?php
.....
?>
<form action="<?php myfunction() ?>">
...
...
</form>
<?php
....
?>

Categories