What does .php?action.. do? [duplicate] - php

This question already has answers here:
what does a question mark mean before a php form action
(4 answers)
Closed 2 years ago.
Can someone explain to me what is the use of ?action=add&code= and what they do in the code below? I have tried to search it on Google but they gave me HTML action atribute instead.
<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
Sorry for the noob question.Thanks for the reply.

Those are called query string values or parameters, they are one of several potential parts of a URL. Each key/value pair provides information that the server-side code can use when constructing the response to send back to the client. (Or the server-side code could even simply ignore them, they have no harmful effect.)
For example, given this key/value pair on the query string:
action=add
In the server-side code you can get the value "add" by fetching it from the query string by its key:
$action = $_GET["action"];
// $action now contains the string "add"
Presumably the logic in the code would then do something based on that value.

action is the name of a "normal" GET variable $_GET['action'].
You must look in the further code to see where it appears and what it is used for.
There is no standard for that

In the url after ? we can pass the values onto another webpage which can be used further.

Related

Why echo is necessary in php inside html files, in this example? [duplicate]

This question already has answers here:
What is the difference between get_the_* and the_* template tags in wordpress?
(3 answers)
Closed 4 years ago.
I wonder:
why I this code is valid:
go to this post
but for retrieving the following value I must use echo, or else it won't work:
back to homepage
I took a glance at get_option documentation and it said that:
Return# - (mixed) Value set for the option.
So perhaps that is the different, that the return value of this function is not a string?
the_permalink() calls echo within the function call. See the full source here. get_option() only returns a value so you have to echo it explicitly if you want it in the html.
I' m not sure, but if you review the the_permalink(); method's body you may see echo command at the end of the method while get_option('home'); just return a string as the result.

Passing an argument from HTML Form to PHP [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Here is the HTML Form:
<form action="imagematch.php" method="get">
Input Route Number: <input type="text" name="N" />
<input type="submit" />
</form>
And here is the PHP file:
<?php
if(isset($_GET[ā€™Nā€™]))
{
$N = $_GET[ā€™Nā€™];
}
?>
Right now, it is reading the PHP file but it isn't reading the "N" argument correctly.
As best we can tell, the issue seems to be the type of quotes you are using around the letter 'N'. This is very common when copying code from webpages. The angled/curly quotes used in webpages aren't interpreted the same as quotes expected and accepted in most programming languages. If you look at this article, you can see the difference. Note you want to do the opposite of what they recommend as they are making recommendations for publications and not code. You want to use straight quotes only in code.
As a separate recommendation, as mentioned by joeDaigle, you should use the GET method for when you are GETting (or reading) information, and use PUT when PUTting (or writing/updating) information. You can read this RFC for more details, but the main reason is that browsers treat GET and POST requests differently (note when you refresh a regular webpage, versus when you refresh a page after submitting a POST form and your browser prompts asking if you're sure).

send the variable to another page in php [duplicate]

This question already has answers here:
How to pass variables received in GET string through a php header redirect?
(8 answers)
Closed 7 years ago.
I have two pages in php ,one of them is exam page(exam.php) and another is result page(result.php), the result is calculated in exam page and must be sent to result page to display.(I don't have a form)
to send the result, Inside exam.php ,I write, header("location:result.php?result");
and to get the result inside result.php ,I write, $newresult=$_GET['result'];
but I receive error,and result didn't sent to the result page.
would you please guide me?
Using a URL to pass parameters can be done like so.
HTML
<a href='yourPage.php?name=Script47'>Send Variable</a>
PHP
<?php
if (isset($_GET['name') && !empty(trim($_GET['name'])) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
}
?>
Explanation
The HTML is fairly simple, we create a link which holds a parameter specified after the page extension (?name=[...]).
The PHP first checks if the name parameter which was passed isset to prevent an undefined index error, and we check if it isn't empty. The trim function removes white spaces so an string with a space isn't outputted (" "). When we know that the string has a value in it we sanitize it (never trust user input) and then we output it.
Reading Material
htmlspecialchars();
trim();
empty();
isset();
Try use session
exam.php
<?php
session_start();
$_SESSION['result'] = $result;
?>
result.php
<?php
session_start();
echo $_SESSION['result'];
?>

Preventing form empty submit [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if multiple strings are empty
today i got this answer here on stackoverflow:
<input type="text" name="required[first_name]" />
<input type="text" name="required[last_name]" />
...
$required = $_POST['required'];
foreach ($required as $req) {
$req = trim($req);
if (empty($req))
echo 'gotcha!';
}
This is ok, but what if someone change
name="required[first_name]"
To
name=""
Then i will have some data missing in further code (i use form to send submited data to email). How to fix this?
Yes, someone can change the html that submits to your code. So you have to check for the existence of everything you want to have in the code that handles the form. Lots of beginners want to automate that away by looping through $_POST or $_GET. And they almost always miss something or end up with code just as complicated, but harder to read, than just checking each input you want.
Loop through the $_GET array and check if any variables are "" or start with something other than required, and then just error out.
You should always validate data on the server side (i.e. in PHP).
You should list the required field in PHP and check them in PHP.
Never trust user data.

Passing PHP variable in a javascript function [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
<button class="addToPlaylist" onclick="javascript:myPopup(<?php echo $videos[$counter]?>);
return false;">+</button>
I have a button on an image as a html hyperlink. I want to perform different actions on hyperlink and button. The above code works whenever I do not pass the PHP variable using echo. When i pass PHP variable, the button also performs the same action as of the hyperlink, that means return false does not work.
Any idea why the return false; does not work when i pass PHP variable?
This should be:
<button class="addToPlaylist" onclick="javascript:myPopup('<?php echo $videos[$counter];?>');return false;">+</button>
Note the single quotes in myPopup. As you pass a string to myPopup, you will need to enclose it with single quotes. (Double won't work as there is already double quotes for the onclick)
I am quite sure $videos[$counter] is not numeric, but a string. In this case you have to write the quotes:
onclick="javascript:myPopup('<?php echo $videos[$counter]?>');
And make sure, $videos[$counter] doesn't contain any, something like
onclick="javascript:myPopup('<?php echo addslashes($videos[$counter])?>');
comes to mind.
onclick="javascript:myPopup("";return false;" . This should work and i think it's more clear where you have javascript code and php code.

Categories