How to Print a variable in a function in PHP - php

I have a form, with post="ModelSelector", when submitted, we go through these codes.
Issue I'm facing is, I want to check the value of $_POST, I know it is getting set by calling "isset()".
I just want to Print/alert/pushout the variable $productselection
function selectProduct() {
// save the post in a variable
$ProductSelections = $_POST['ModelSelector'];
// I want to print $ProductSelection to check its value
$frmVars['ProductSelections'] = $ProductSelections;
$frmVars['WindowSize'] = $WindowSize;
$frmVars['PageNum'] = 1;
saveFormValues(0,'RunDefMgr', $frmVars);
// Clear the checkboxes
$sel = array();
deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}
if(isset($_POST['ModelSelector'])) {
selectProduct();
}
I have tried ECHO, for some reason it is not printing the value in HTML.
Thanks in advance.

I want to check the value of $_POST
$_POST will be an array.
Use print_r($_POST) or var_dump($_POST) to view its contents.

Your form method should be method="POST", You can use the following edit to see if it works , as you have to pass $_POST (array) to the function to use it inside function. function expects an parameter else , $_POST does not exists.
And also enable the errors inside your file to check which type of errors you are getting by using : ini_set('display_errors',1); or error_reporting(E_ALL);
function selectProduct($_POST) { // create parameter $_POST which we get from isset condition.
// save the post in a variable
$ProductSelections = $_POST['ModelSelector'];
print_r($ProductSelections); // print the value.
// I want to print $ProductSelection to check its value
$frmVars['ProductSelections'] = $ProductSelections;
$frmVars['WindowSize'] = $WindowSize;
$frmVars['PageNum'] = 1;
saveFormValues(0,'RunDefMgr', $frmVars);
// Clear the checkboxes
$sel = array();
deleteRunDef(0,"*","RUN_DEF_EDIT","*");
}
if(isset($_POST['ModelSelector'])) {
selectProduct($_POST); // pass the $_POST array to the selectProduct function.
}

Related

Handle a PHP error message when a second node doesn't exist?

Hope someone can help with this
I'm using this to grab the first part of my URL
$page_url = perch_page_url(['include-domain' => false,], true); //
Output the URL of the current page, minus https
$url_parts = explode("/", $page_url); // Split a string by a string
I'm using this technique to grab the first and second nodes of the URL
$first_node = $url_parts[1]; // First part of string
$second_node = $url_parts[2]; // Second part of string
On my homepage, there isn't a second node, so I get an undefined offset message.
Is there a way to check if the $second_node exists?
I've tried using
if (isset($second_node)) {
echo "$second_node is set so I will print.";
}
and
if (!empty($second_node)) {
echo '$second_node is either 0, empty, or not set at all';
}
Both if statements only echo after the $second_node has been set? So I still get the error message.
You can do something like this:
$second_node = isset($url_parts[2]) ? $url_parts[2] : FALSE;
if ($second_node)
{
echo "Second node: {$second_node}";
}
This checks if the third index (0-based) of the $url_parts array is set. If yes, it will assign its value to $second_node. If not, it will assign FALSE so you can handle that further down in the code if you need to check it later (again).
That is because the error is thrown at the initialization of your $second_node variable.
Check if the node exists first, and then declare the variable:
$second_node = ""; // Or whatever
if (isset($url_parts[2]) {
$second_node = $url_parts[2];
}
Check it here: https://3v4l.org/fRJ7L
Please use it directly in the if condition and avoid assigning to a variable or assign after checking it in if condition
if (isset($url_parts[2])) { // remove this line $second_node = $url_parts[2];
echo "$second_node is set so I will print.";
}

Is there a php function which returns whether the query string is empty?

I'm designing a semi-basic tool in php, I have more experience server side, and not in php.
My tool contains 10-15 pages, and I am doing the navigation between them with the $_GET parameter.
I would like to check if the query string is empty (to know if I'm in the home page). Is there any php function for this ? Of course I can do it manually, but still?
EDIT: My question is if there is a function that replaces
if(! isset("param1") && .....&& ! isset("paramN")){
...
}
Try below
if(isset($_GET['YOUR_VARIABLE_NAME']) && !empty($_GET['YOUR_VARIABLE_NAME'])) {
}
isset() is used to check whether there is any such variable or not
empty() to check whether the variable is not empty or not
As per your comment, assume your URL as below
http://192.168.100.68/stack/php/get.php?id=&name=&action=delete&type=category
And your PHP script as below
<?php
$qs = $_GET;
$result = '';
foreach($qs as $key=>$val){
if(empty($val)){
$result .= 'Query String \''.$key.'\' is empty. <br />';
}
}
echo '<pre>'; print_r($result);
?>
In my above URL I passed id and name as empty.
Hence, Result will be like below
id is empty.
name is empty.
but I dont think its standard way.
If you want to process something only if all parameters are having some values, they you can move those process inside a if as below
if(empty($result)) {
// YOUR PROCESS CODE GOES HERE
} else {
echo 'Some Required Parameters are missing. Check again';
}

session array won't unset

I have this small script that runs after a user posts.
I have a $_SESSION['con29'][value] that I populate with data from the post.
This code should either add or remove an array element.
It adds an element fine, but I can't work out why it won't unset the $_SESSION array
if (isset($_POST['sub_search_content_id']))
{
$row_id_1 = $_POST['sub_search_content_id'];
$_SESSION['con29'][$row_id_1] = $_POST['sub_search_content_id'];
}
if (isset($_POST['sub_search_content_id_remove']))
{
$ss_id = $_POST['sub_search_content_id_remove'];
unset($_SESSION['con29'][$ss_id]);
}
Use !empty instead isset
because isset only will check variable is created or not
but !empty will check variable is empty or not too.
and if you don't have another session variables so you may try
session_unset();or session_destroy();
and you can also use
session_register($value);
and
session_unregister($value);
for this
Hope this helps

Unable to create Dynamic Variables

When I check my cookies I do see that PHPSESSID is being made. But when I try to use them I get an error message saying that the variable is undefined.
This is how I set my Sessions:
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
if (isset($_POST[$post])) {
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
}
}
I also tried it manually like:
$_SESSION['auto_year'] = '2012';
But it still doesn't work. I did call session_start() on top of the pages of both but it just keeps giving me that error.
using ${} you can create dynamic variables. In your case just change to:
${$post} = $_SESSION[$post];
Another option is to create the variables using extract() which enables you to set all variables in one go;
extract($_SESSION);
Will create a variable for each key in the session, e.g. $auto_year, $auto_brand, etc.
http://php.net/manual/en/function.extract.php
note
I'm wondering why you want to have all data in separate variables, whereas a single associative array may be just as easy to handle?
It's likely something wrong with the $_POST data coming from your form. As I don't have form info, I tried this by setting $_POST.
session_start();
$_POST['auto_year'] = 1977;
$posts = array("auto_year", "auto_brand", "auto_model", "auto_bodywork", "auto_doors",
"auto_fuel", "auto_gearbox", "auto_type", "auto_uitvoering", "auto_part", "auto_description", "email_address");
//Define POSTS and set into SESSIONS
foreach ($posts as $post) {
echo $post."<br>";
if (isset($_POST[$post])) {
echo "Creating variable for $post<br>";
$_SESSION[$post] = $_POST[$post];
$$post = $_SESSION[$post];
echo '$auto_year: '.$auto_year . "<br>";
}
}
echo "<br><br>".var_dump($_SESSION);

isset won't identify parameters in url

I have the following code in my file.php file:
if (isset($_POST["activate"])){
$confirmed = true;
$result = execute_query("UPDATE tributes SET t_confirm = 1 WHERE t_id=".$_POST["tid"]." AND t_activation='".$_POST["activate"]."'");
if($result){
}
}else{
print "NO";
}
I call this file throught he following url:
http://localhost/ccmta/tribute.php?tid=55&activate=QiScE8W76whfQD0Twd15enG31yDEf1iVGLL0SHEB9doqI16bd8kskOPXu6bGZE65o7XPp9EXUBCJS7IbcjNZ98hA8vR11b0Ve0Qm
but the isset function won't recognize the activate variable that's in parameter in the URL and falls into the else bracket. I've also called print_r to see what is in the $_POST variable and it's an empty array. Any idea why?
Yes - $_POST is the array of POST, not GET (query string/URL) data. If you want both, use $_REQUEST, otherwise, use $_GET.

Categories