hide element by checkbox html and php only - php

I wanna make a checkbox that when user check and submit its appear specific elements.
this is the selection form in admin.php
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
and the selected part will be shown in user.php. each option has its own php file so I got 3 files adv.php, ann.php and pic.php. and using <?php include 'adv.php'; ?> to show in user.php
any suggestion how to do it ??

Update your form like this;
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
And at the backend;
$pages = array ("adv", "ann", "pic");
$page_type = $_GET["page_type"];
// Validate page type
if (in_array($page_type, $pages)) {
include $page_type . ".php";
} else {
die("Invalid page type");
}

Related

How to get two html forms working in one .php file?

Now i have 4 buttons on my page. They define different parameters. What i want to do, is use php switch case, and after pressing for example button1 i want the datetimepicker, and a chart appear as well. I read that i can manage with that problem using two html forms in one php file adding form id, but im only getting the first form to work properly while the second one seems to fail with sending the data using POST method. Below how i try to do this:
<form id="switch" action="data-from-database.php" method="post">
<h2 align="center">Wybierz interesujący Cię przedział
czasowy</h2></br>
<div id="pudlo">
<input type="submit" name="przycisk"
class="input_wykresy" value="temperatura" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="wilgotność" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="ciśnienie" form="switch">
<input type="submit" name="przycisk"
class="input_wykresy" value="irradiancja" form="switch">
</div>
</form>
<?php
//PHP part here is what i want to appear on the page after using switch
switch ($_REQUEST['przycisk'])
{
case "temperatura":
//Once again HTML part
?>
<form id="picker" action="data-from-database.php" method="post">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" name="filter" id="filter" value="Wyszukaj"
form="picker" class="input_wykresy"/>
</form>
<?php
if (isset($_POST["from_date"], $_POST["to_date"])) // php code which i want to work in case
{
$handle = $link->prepare("SELECT temperatura, czaspomiaru FROM Pomiary
WHERE czaspomiaru BETWEEN'" . $_POST["from_date"] . "' AND
'" . $_POST["to_date"] . "'");
}
$dataPoints = [];
//Best practice is to create a separate file for handling connection to database
try
{
// Creating a new connection.
// Replace your-hostname, your-db, your-username, your-password according to your database
$handle->execute();
$result = $handle->fetchAll(\PDO::FETCH_OBJ);
if ($result)
{
foreach ($result as $row)
{
array_push($dataPoints, ["label" => $row->czaspomiaru, "y" =>
$row->temperatura]);
}
}
else
{
?>
<div>
<h2>
<center>Nie znaleziono pasujących rezultatów</center>
<h2>
</div>
<?php
}
$link = null;
}
catch (\PDOException $ex)
{
print($ex->getMessage());
}
}
First form working properly, which i tested on buttons. Second one doesnt work, because when i press the submit button refering to datetimepickers, nothing shows on chart i assume no data been sent. I wonder how can i get the second form working?
You might want a hidden field holding the name/value pair of the button taking you to the subform since the button is not part of that form.
<form id="picker" action="data-from-database.php" method="post">
<input type="hidden" name="przycisk" value="temperatura">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" name="filter" id="filter" value="Wyszukaj"
form="picker" class="input_wykresy"/>
</form>
Or, since you do not use the submit button name="filter" in your PHP code, you could just alter that button.
<form id="picker" action="data-from-database.php" method="post">
<input type="text" name="from_date"
id="from_date" class="input_wykresy1" placeholder="Od" autocomplete="off">
<input type="text" name="to_date" id="to_date"
class="input_wykresy1" placeholder="Do" autocomplete="off">
<input type="submit" id="filter" name="przycisk" value="temperatura"
form="picker" class="input_wykresy"/>
</form>
Consider using four radio buttons (instead of four submit buttons). Your site visitor(s) can select which radio button and submit. The code could easily be handled with if, elseif, else conditions, and the output could appear in one designated area, regardless of which radio button was selected.
<form id="switch" action="data-from-database.php" method="post">
<h2 align="center">Wybierz interesujacy Cie przedzial
czasowy</h2></br>
<div id="pudlo">
<input type="radio" name="przycisk" class="input_wykresy" value="temperatura">
<input type="radio" name="przycisk" class="input_wykresy" value="wilgotnosc" >
<input type="radio" name="przycisk" class="input_wykresy" value="cisnienie" >
<input type="radio" name="przycisk" class="input_wykresy" value="irradiancja" >
<input type="submit" value="submit">
</div>
</form>
<?php
if (przycisk === "temperatura") {
echo "yada";
} elseif (przycisk === "wilgotnosc") {
echo "yada";
} elseif (przycisk === "cisnienie") {
echo "yada";
} else {
echo "yada";
}
?>

PHP - Store input values in external php array

I would like to store an user input from a textbox into an external php array by clicking a button. Also, there is a dependency from two radio buttons.
This is my current attempt:
At first, I include the external php array:
<?php
include ('../array.php');
?>
The input form:
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
Insert the value into the array:
<?php
if (isset($_POST['send'])){
if (isset ($_POST['name'])){
if ($_POST['name']=='Type1'){
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
}
?>
But the values don't "stack", meaning that the array isn't growing with each button click.
Can anyone help please? :D
Thanks in advance!
If you want to stack values, you need to store them somewhere to keep values between each requests to server.
Values can be stored in session (for current user only) or in any database supported by PHP.
Here an example with session :
index.php :
<?php
include ('./array.php');
?>
<form action="" method="post">
<input type="text" name="textfield" value="">
<input type="radio" name="name" value="Type1">Type1<br>
<input type="radio" name="name" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
array.php :
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['send']) && isset($_POST['name']) && $_POST['name']=='Type1') {
$_SESSION['persistentValues'][] = $_POST['textfield'];
}
print_r($_SESSION['persistentValues']);
?>
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
in the file php put this
<?php
if (isset($_POST['send']) && $_POST['textfield'] && $_POST['name']){
if ($_POST['name']=='Type1'){
array = require('./array.php');
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
?>

How to get customized attributes value of a html radio button in php

<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>

How do I make a form update itself?

I have a form with 3 selections, and I want the form to replace the selected text with the name entered when submitted. So if the user enters their name as Josh, and picks 'King', I want the form to update itself and instead of having King it would say Josh which would no longer be a choice, and then have the other two characters below for someone else to pick from. This is a PHP document, but the data is in a HMTL form of course.
I was reading something about hidden forms, but was not sure how to implement them here. Does anyone have any suggestions?
<form method="get" action="">
Your Name: <input type="text" name="name" />
<br/>
<input type="radio" name="character" value="King"> King<br>
<input type="radio" name="character" value="Queen"> Queen<br>
<input type="radio" name="character" value="Prince"> Prince <br>
<input type="submit" value="Submit" />
Are you looking to do something like this?
You can do it by using php conditions and hidden fields.
Save the file with the name "form.php" then run it and tell me if it does the trick ;)
<form method="post" action="form.php">
Your Name: <input type="text" name="name" />
<br/>
<?php if((isset ($_POST['name-1']) || isset($_POST['name'])) && (isset ($_POST['character-1']) && $_POST['character-1']=='King'))
{
$name=isset($_POST['name-1'])?$_POST['name-1']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-1" value="'.$name.'"/><input type="hidden" name="character-1" value="'. $_POST['character-1'].'"/>';
}
else {?>
<input type="radio" name="character-1" value="King"> King<br>
<?php } ?>
<?php if((isset ($_POST['name-2']) || isset($_POST['name'])) && (isset ($_POST['character-2']) && $_POST['character-2']=='Queen'))
{
$name=isset($_POST['name-2'])?$_POST['name-2']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-2" value="'.$name.'"/><input type="hidden" name="character-2" value="'. $_POST['character-2'].'"/>';
}
else {?>
<input type="radio" name="character-2" value="Queen"> Queen<br>
<?php } ?>
<?php if((isset ($_POST['name-3']) || isset($_POST['name'])) && (isset ($_POST['character-3']) && $_POST['character-3']=='Prince'))
{
$name=isset($_POST['name-3'])?$_POST['name-3']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-3" value="'.$name.'"/><input type="hidden" name="character-3" value="'. $_POST['character-3'].'"/>';
}
else {?>
<input type="radio" name="character-3" value="Prince"> Prince <br>
<?php } ?>
<input type="submit" value="Submit" />
</form>

Form inside another form with same inputs

Is there a way to send exactly same variables to another page different from action of the form? I tried this structure but the second submit button did not work;
<form name="2" action="page2" method="post" >
<form name="1" action="page1" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1">
</form>
<input type="submit" value="Submit2">
</form>
The form 1 shows the information of inputs on the page1 and updates database also.
I want form 2 to show the information of inputs on the page2 only (no update of database).
Is that possible?
You cannot have form inside another form , change your code like this.
<form name="2" action="page2" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1" name="submit1">
<input type="submit" value="Submit2" name="submit2">
</form>
Give different name for each submit button.
in php
if(isset($_POST['submit1'])){
// submit1 is pressed
}
if(isset($_POST['submit1'])){
// submit2 is pressed
}
Changing action dynamically.
add class to submit buttons say, class="submit". ANd add id to form say id="my-form"
$(".submit").change(function() {
var action = $(this).val() == "submit1" ? "submit1.php : "submit2.php";
$("#my-form").attr("action", action);
});

Categories