How to Set $_Get Variable

How to set $_GET variable

You can create a link , having get variable in href.

<a href="www.site.com/hello?getVar=value" >...</a>

assign a new value to a $_GET variable

$_GET is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that come with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.

So if you have a request like mypage.php?key=value, PHP automatically does something equal to this for you:

$_GET['key'] = 'value';

And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..

PHP - $_GET variable not set

I've decided to make the processor look less confusing by adding a hidden input tag in the view to differentiate between create and update that way. So viewCountries.php stays the same.

createCountry.php

<input type="hidden" name="Action" value="Create">

editCountry.php

<input type="hidden" name="Action" value="Update">

countryProcessor.php

// Create Country
if($_POST['Action'] == "Create") {

if(isset($_POST['CountryName'])) {
$CountryName = $_POST['CountryName'];
}
if(isset($_POST['Gross'])){
$Gross = $_POST['Gross'];
}

$stmt = oci_parse($conn, "INSERT INTO COUNTRY (COUNTRYNAME, GDP) VALUES (:CountryName, :GDP)");

oci_bind_by_name($stmt, ":CountryName", $CountryName);
oci_bind_by_name($stmt, ":GDP", $Gross);

oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);

oci_free_statement($stmt);
oci_close($conn);

// echo $Gross;
// echo $CountryName;
if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=$CountryName has been created!");
} else {
header("Location: ../viewCountries.php?Danger=$CountryName hasn't been created!");
}

// Update Country
} elseif($_POST['Action'] == "Update") {

if(isset($_POST['CountryID'])) {
$CountryID = $_POST['CountryID'];
}
if(isset($_POST['CountryName'])) {
$CountryName = $_POST['CountryName'];
}
if(isset($_POST['Gross'])){
$Gross = $_POST['Gross'];
}

$stmt = oci_parse($conn, "UPDATE COUNTRY SET COUNTRYNAME = :CountryName, GDP = :GDP WHERE COUNTRYID = :CountryID");

oci_bind_by_name($stmt, ":CountryID", $CountryID);
oci_bind_by_name($stmt, ":CountryName", $CountryName);
oci_bind_by_name($stmt, ":GDP", $Gross);

oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);

oci_free_statement($stmt);
oci_close($conn);

// echo "CountryID" . ' ' . $CountryID . "<br>";
// echo "GDP" . ' ' . $Gross . "<br>";
// echo "Country Name" . ' ' . $CountryName . "<br>";
// echo "Rows Affected" . ' ' . $Affected;

if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=$CountryName has been updated!");
} else {
header("Location: ../viewCountries.php?Danger=$CountryName hasn't been updated!");
}

} else {

// Delete Country
if(isset($_GET['CountryID'])) {

$CountryID = $_GET['CountryID'];

$stmt = oci_parse($conn, "DELETE FROM COUNTRY WHERE COUNTRYID = :CountryID");

ocibindbyname($stmt, ":CountryID", $CountryID);

oci_execute($stmt);
$Affected = oci_num_rows($stmt);
oci_commit($conn);

oci_free_statement($stmt);
oci_close($conn);

if(count($Affected) > 0){
header("Location: ../viewCountries.php?Success=Country has been deleted!");
} else {
header("Location: ../viewCountries.php?Danger=Country hasn't been deleted!");
}
}

}

PHP GET variable not being set

You used POST method of ajax. So send data also in POST manner like below:-

// Send the login/registration data to database
$(document).ready(function() {
var username = $("#usernameField").val();
var email = $("#emailField").val();
var password = $("#passwordField").val();
$.ajax({
type: "POST",
url: "../index.php",
data: {"username":username,"email":email,"password":password,"action":"register"},
success: function (result) {
alert(result);//check the change
}
});
});

And then change GET to POST at php end:-

<?php 

require_once("Model/model.php");
require_once("Controller/controller.php");
require_once("View/view.php");

$model = new Model();
$view = new View();
$controller = new Controller($model, $view);

$controller->Begin();

// Client wants to register
//single condition can do the job and use POST instead of GET
if(isset($_POST['action']) && $_POST['action'] == "register" ) {
echo "hello"; //check the change
}
?>

Note:- Please take care of comments too.(added in the code)

How to increase/decrease the number in $_GET variable link?

Assuming your images will always be image1.jpg, image2.jpg, image3.jpg etc, you can achieve what you're looking for like this:

Note: I used PHP only just so it's easier to follow the logic

<?php

//Check $_GET variable exists otherwise set it to a default value = 1

if(isset($_GET['link'])) {
$link = $_GET['link'];
} else {
$link = 1;
}

if($link == 1) { $linkURL = "http://website.com/image1.jpg";}
if($link == 2) { $linkURL = "http://website.com/image2.jpg";}
if($link == 3) { $linkURL = "http://website.com/image3.jpg";}

// Set variables for the previous and next links
$prev = $link-1;
$next = $link+1;

//Display your image
echo "<img src='".$linkURL."'></img>";

//Only show the previous button if it's NOT the first image
if($prev!=0){
echo "<a class='prevBtn' href='http://website.com/page.php?link=".$prev."'>Previous</a>";
}

//Only show the next button if it's NOT the last image
if($next<=3){
echo "<a class='nextBtn' href='http://website.com/page.php?link=".$next."'>Next</a>";
}
?>

Set $_GET['foo'] variable when calling a PHP script on CLI

No, Its not possible via CLI. However you can manually assign the value to the $_GET variable.

OR you can use the command line arguments and assign them to the $_GET.

$_GET['data'] = $argv; 

^^That's a little bit manageable..

Pass entire URL to $_GET variable in PHP built-in server

You could bootstrap your application with this script. Save this snippet to a file, and set it as your entry point in whatever web server software you're using. It will produce the results you're asking for.

<?php
$root=__dir__;

$uri=parse_url($_SERVER['REQUEST_URI'])['path'];
$page=trim($uri,'/');

if (file_exists("$root/$page") && is_file("$root/$page")) {
return false; // serve the requested resource as-is.
exit;
}

$_GET['url']=$page;
require_once 'index.php';
?>

Check if php get variable is set to anything?

You can try empty.

if (!empty($_GET['variable'])) {
// Do something.
}

On the plus side, it will also check if the variable is set or not, i.e., there is no need to call isset seperately.

There is some confusion regarding not calling isset. From the documentation.

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

and...

That means empty() is essentially the concise equivalent to !isset($var) || $var == false.



Related Topics



Leave a reply



Submit