Getting value of clicked element in PHP - php

I have a page with element <a> acting like a <button>, in this form the elements works like a service plans, and i want to get a value when one of these buttons has been clicked.
Page1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page 1</title>
<?php
session_start();
?>
</head>
<body>
<section>
Choose Plan1
<br><br>
Choose Plan2
<br><br>
Choose Plan3
</section>
</body>
</html>
Page2.php
<?php
session_start();
echo 'test';
echo $_POST['name'];
?>
I know that $_POST['name'] doesn't works cuz don't exist a input, but i don't know how use something like this to get a value of button clicked and show this value.
The return expected for first button for example, is something like:
basic
I need this value to put in database after.
Someone can help me to get this values?

You may pass the value as a url parameter
<section>
<a href="page2.php?plan=basic" >Choose Plan1</a>
<br><br>
Choose Plan2
<br><br>
Choose Plan3
</section>
And get it from $_GET in your php code
echo $_GET['plan'];

Related

I'm trying to figure out how to get the variable $_GET to work so that the H1 tag turns into what the user imputted

The question I'm trying to answer is below
Create a new PHP file named lab2.php.
Inside, add the HTML skeleton code and give its title “Lab Week 2"
Within the body tag, create a h1 tag that says "Enter the name of your favourite musical artist"
Add a section tag inside the body tag.
Create a form tag and use the $_GET method to get users to input their favorite artist. Store the user input into variables.
Create a submit button for the user to input their favorite artist.
Check whether the variable is empty or not.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Fav Artist</title>
<meta name="author" content="Sean Kingston">
<meta name="viewport" content="with=device-width, initial-scale=1.0">
</head>
<body>
<!-- H1 tag I want to change once the user clicks on submit button-->
<h1> Enter the name of your favourite musical artist </h1>
<!--Form-->
<form method="get">
<input type="text" name="name" />
<input type="submit" value="Submit" name="sub">
</form>
</body>
</html>
<?php
//isset not working//
if(isset($_GET['name'])){
echo $_GET['name'];
}
?>
<?php
show_source(FILE);
</html>
?>
i'm gonna hazard a guess and asume that this is some sort of educational task.
but nevertheless here is how i understand the tasks.
<?php
//1.Create a new PHP file named lab2.php.
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Lab Week 2</title><!-- 2.Inside, add the HTML skeleton code and give its title “Lab Week 2" -->
</head>
<body>
<h1><?php
// 5. use the $_GET method to get users to input their favorite artist. Store the user input into variables.
$favouriteMusicalArtist = $_GET['favourite_musical_artist'];
// 7.Check whether the variable is empty or not.
if (empty($favouriteMusicalArtist)) {
//3.Within the body tag, create a h1 tag that says "Enter the name of your favourite musical artist"
echo 'Enter the name of your favourite musical artist';
} else {
echo 'Your favourite musical artist is: ' . $favouriteMusicalArtist;
}
?></h1>
<!-- 4.Add a section tag inside the body tag. -->
<section>
<!-- 5.Create a form tag -->
<form method="GET">
<input type="text" name="favourite_musical_artist"/>
<!-- 6.Create a submit button for the user to input their favorite artist. -->
<button type="submit">Submit</button>
</form>
</section>
</body>
</html>
Now if this is some sort of test don't just copy of the internet. Do some fiddling around with the code and try to learn something :-)

how to redirect to a php file within an html select tag

I am trying to make an inex.php file as a dashboard page that shows a selection of files that are part of the folder that index.php is part of. and when the user selects the file, I want it to redirect to that page. I am using xammp as a local webserver for this.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<form action="" method="POST">
<label for="pages">page:</label>
<select name="pages" id="page">
<?php
foreach (glob("*.php") as $filename) {
echo "<option value='strtolower($filename)'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I am using a foreach to get all the files that are in the folder and populate them in a selection menu. I want to know how I can redirect to those files when I hit submit.
Your code is correct, you need only remove strtolower method from value, and use onchange on select tag.
Edit, Second Sol when submitted form :
<form action="" method="POST" onsubmit="return submitForm()">
<label for="pages">page:</label>
<select name="pages" id="page" >
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" id='submitBtn' value="Submit">
</form>
<script type="text/javascript">
function submitForm(){
window.location.href = document.getElementById("page").value;
return false;
}
</script>
First sol,Try it:
<select name="pages" id="page" onchange=" (window.location = this.value);">
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
Don't use a select to redirect, use a <a> tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<h1>pages</h1>
<ul>
<?php
foreach (glob("*.php") as $filename) {
echo "<li>" . $filename . "</li>";
}
?>
</ul>
</body>
</html>
// edit: wrong concat operand

Is there a solution to redirect a page with a select option from the database that i added with a input field?

I am trying to create a option list , using a input field. The input field submits the data to my databse and in the same time i create a new file with the name of the value that i added in the input field and attach a exetension of ".php" to create a php file. Also together with the value i send also the file path into the database. In the index.php you can see the following code:
INDEX.PHP
<?php
include "conn.php";
$query="SELECT DISTINCTname,filePath FROM name_data";
$res=mysqli_query($conn,$query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<select name="select_list" id="list" onchange="location=this.value">
<?php foreach($res as $name){?>
<option value="<?php echo $name['filePath'];?>"><?php echo $name['name'] ;?></option>
<?php };?>
</select><br><br>
<form action="set.php" method="post">
<input type="text" name="name"><br><br>
<input type="submit" name="submit"><br><br>
</form>
</body>
</html>
And for the form submission i use the following code. Now in this example i dont use RegEx for the input field its just a simple code that i am trying:
SET.PHP
<?php
include "conn.php";
if(isset($_POST['submit'])){
$name=$_POST['name'];
$path=$name.".php";
$query="INSERT INTO name_data(name,filePath) VALUES('$name','$path')";
if(mysqli_query($conn,$query)){
header("LOCATION:index.php");
}
fopen($path,"w");
};
Now What i want to do, when i query the database i want the value(name) from the form submission to be linked to the corresponding file path(file Path) that i created during form submission.

Get html value to pass to php withini the file

How can I get an HTML value to send to PHP. I would like to avoid using a form. For example I have an input:
<input name="input" id="input">Input</input>
while in PHP:
$input = $_POST['input'] --> but didn't work
or
$input = $_GET['input'] --> still didn't work
I know that I will be able to get it using form then action="another file" but I want it within a file. Please help. Thank you.
If you simply want to use js to append the input value in url variable then you can use js function as follows;
client.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<a onclick="append()" id="anch" href="server.php?text">Send value</a>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
anch.href=anch.href+"="+txt.value;
}
</script>
server.php
$variable = $_GET["input"];
echo $variable;
Output on localhost:
After clicking send value:-
Update: I just read the end of your question. If you want to show the value of input on same page/file then you can use following code;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<?php
if(isset($_GET['text']))
{
$text=$_GET['text'];
echo "<a onclick='append()' id='anch' href='?text=$text'>Enter Value</a>";
}
else
{
echo "<a onclick='append()' id='anch' href='?text'>Enter Value</a>";
}
?>
<br>
<?php
if(isset($_GET['text']))
echo "<div id='values'>".$_GET['text']."<div>";
?>
</body>
</html>
<script>
function append()
{
txt=document.getElementById('txt');
anch=document.getElementById('anch');
if(anch.href.match(/=/)=="=")
anch.href=anch.href+txt.value+"<br>";
else
anch.href=anch.href+"="+txt.value+"<br>";
}
</script>
Output:
Check output on phpFiddle
It seems that the way to go is to use the GET method.
You will need to access your page with the get parameters included like this:
http://localhost/index.php?input=VALUE_HERE
And in your php file:
$variable = $_GET["input"];
echo $variable;
But if you insist to use inline html elements to get data, you need to use javascript for that like this for jquery:
alert("This is the value of the input" + $("#input").val());
Don't use this
<input name="input" id="input">Input</input>
Use this
<input name="input" id="input">

Add default email variable in link URL

I want to add a variable Email address into the link (so email address dynamically fills when someone hit the link), which re-direct PHP. Email addresses are different based on person visited the link. So I am wondering, how can I in ordinary URL address add variable Email, which will then intervened and added to PHP re-direct?
This is the code, but not working...
<?PHP
$subid=$_GET[subid];
$email=$_GET[email];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://xentrk.com/?a=41&c=308&s1={{ email|default:'null' }}<?PHP echo $subid;?>" />
</head>
<body>
You are using echo $subid in your URL:
Surely this should be $email ?
In which case this is the code:
<?php
$subid=$_GET['subid'];
$email=$_GET['email'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://xentrk.com/?a=41&c=308&s1={{ email|default:'null' }}<?php echo $email;?>" />
</head>
<body>

Categories