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">
Related
I have a piece of php code inside html tag which is supposed to change the tag's style in accordance with the contents of the URL.
There is an html login form which looks like this:
<form class="userdata" action="login.php" method="post">
<input type="text" name="email" placeholder="E-mail" <?php fillin('email'); enlight_unfilled('email');?>><br>
<input type="password" name="pwd" placeholder="Password"><br>
<button type="submit" name="login-submit">Login</button>
</form>
Here are the functions fillin and enlight_unfilled:
<?php
function fillin($key) {
if (isset($_GET[$key])) echo "value=".$_GET[$key];
else echo NULL;
}
function enlight_unfilled($key) {
if (isset($_GET['error']))
if (isset($_GET[$key]) and $_GET[$key] !== "") echo NULL;
else echo "style='border-color: red'";
else echo NULL;
}
?>
If I only apply one of the functions within the tag, they both do what they are expected to – either save the email in the field if it has been already typed in or enlighten the email field if it has been left empty. But if I apply them together, when the field is empty, php assigns the field value 'style='border-color:. I also tried to use functions like print and printf, but the result is the same:
I am a beginner at php coding and mixing it with html, so the question may appear to be dumb, but I did not manage to find any sort of a solution to this issue, so thanks for help and patience in advance!
It looks like you don't properly encase value in quotes, so it just renders the 'style='border-color:.
Let's assume that $_GET[$key] has a value of hello#hello.com. What your PHP & HTML renders is the following:
value=hello#hello.com
See the problem? There are no quotes. That's why the renderer goes forward searching for a valid value. To fix the issue you must add quotes around your $_GET[$key] in the fillin function. Something like this should do the job:
if (isset($_GET[$key])) echo "value='".$_GET[$key] . "'";
It works when ran alone because it reaches the end > and just assumes the value to be hello#hello.com
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>
I understand how a PHP URL works - I think ... but I'm having problems getting the actual value of the variable to be passed in the example below.
Example
Note: I am adding the below form into a data cell (as part of a table being read via PHP).
$currentrowid = 1;
echo '<td>
<div class="editdelete">
<form action="phpindex.php?page=edit&thisrow=<?php echo $currentrowid;?>" method="post">
<input type="submit" value="Edit" >
</form>
</div>
</td>';
... Some other section of code to read the URL output by the form above:
$val = $_POST['thisrow'];
echo "the value is: " .$val; //Outputs "$currentrowid"
So, as you can see the code returns the actual name of the variable being passed, NOT the value of the variable being passed.
Any ideas here?
Since you are already within a PHP block, you should not wrap your variable within <?php ... ?>. This will give you an error.
To make this work, you can choose 1 of 2 options:
1) String Concatenation:
echo '... <form action="phpindex.php?page=edit&thisrow='.$currentrowid.'" method="post"> ...';
2) Wrap your string in " (double quotes) instead of ' (single quotes):
echo "... <form action=\"phpindex.php?page=edit&thisrow=$currentrowid\" method=\"post\"> ...";
Note that the second method forces you to escape all the double quotes inside of your string.
2 point.
<form action="index.php?thisrow=<?php echo $currentrowid ?>"
method="post">
You should use $_POST not $_GET to get the post value.
As what was answered above,
<form action="index.php?thisrow=<?php echo $currentrowid; ?>" method="post">
is correct.
The reason behind this is you are passing HTML and you have to use an echo from php to output to the html. Otherwise you just get exactly what you put, which is $currentrowid.
Not the easiest, but a quick way to solve your problem. Change your form method to get method="get">, then
$val = $_GET['thisrow'];
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.
I am having a lot of trouble with this, $report is breaking at the space in the URL, and I Have been trying to fix this problem for a couple of days now, and nothing is working.
<form onsubmit="return validate(this)" method="post" action=
<?
echo "\"reports1.php?report=";
echo rawurlencode($report);
echo "\"";
?>
>
...
if(isset($_GET['report'])){
$report = $_GET['report'];
echo "<script>alert('All reports will be appended to \"".$report."\" until GET and POST data are cleared.')</script>";
}
elseif($country != NULL){
$report = $date." ".$country." ".$topic;
}
elseif($country == NULL){
$report = $date." ".$region." ".$topic;
}
...
Here is an example; the $report is getting $_GET'ted as
"2011-05-08 ",
even though it should be
but it is $_POSTING as "2011-05-08 Brazil Botulism"
"reports1.php?report=2011-05-08 "
urlencode() will work.
Make sure you wrap the address in quotes, and it is on one line:
<form ... action="reports1.php?report=2011-05-08%20Brazil%20Botulism">
Use PHP's trim() function to remove unwanted whitespace in the beginning and the end of a string.
well you dont need to raw it,
echo "hi";
will work fine with ya, in case of ur code above there is only one problem that u use $_POST method for ur form not $_GET
form onsubmit="return validate(this)"
method="post" action=
should be
a very nice and easy debugin code that can help u is retriving all $_GETs and assign them to vars,
with
foreach($_GET as $var=>$val)$$var=$val;
this code wil get all posted gets and assign them to variables with there own name, ($report instead of $_GET['report'];) u can use this function on all $_post and $_get to know where is exactly the problem :)..
hope it helps