Results from search not shown in iframe (using switch statement) - php

We are using a switch statement to switch between languages but when we try to use case 1(the dutch/nl flag), the results no longer shows up. Everything works fine without using the switch case, however.
This is our index
<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<h1>Zoeken op Medicijn</h1>
<form method="post" action="tabel.php" target="Mediframe">
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
This is our search function:
<html>
<head>
<title>Medispeak</title>
<link rel=stylesheet href=ProjectCSS.css>
</head>
<body>
<?php
include 'db.php';
print_r($_REQUEST);
$zoek = $_REQUEST['zoek'];
$lang = $_REQUEST['lang'];
if ( empty ( $lang ) ) { $lang = "1"; }
switch ($lang) {
case "1":
echo 'case1';
try
{
$sQuery= "SELECT Medibijsluiter
FROM Medispeak
WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if($oStmt->rowCount()>0)
{
echo '<table border="2">';
echo '<thead>';
echo '<td>Medibijsluiter</td>';
echo '</thead>';
while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>';
echo '<td>'.$aRow['Medibijsluiter'].'</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
echo 'Helaas,geen gegevens bekend';
}
break;
}
}
catch(PDOException $e)
{
$sMsg = '<p>
Regelnummer: '.$e->getLine().'<br />
Bestand: '.$e->getFile().'<br />
Foutmelding: '.$e->getMessage().'
</p>';
trigger_error($sMsg);
}
$db = null;
?>
</body>
</html>

You should use $_GET['lang'] instead of $_POST['lang']. Or you can user $_REQUEST['zoek'] and $_REQUEST['lang'].
If you have form type as GET, then the form data can be accessed using $_GET or $_REQUEST. If the type is POST, you should use either $_POST or $_REQUEST.
Read this to know more about GET, POST and REQUEST in PHP
So change your code
$zoek = $_POST['zoek'];
switch ($_GET['lang']) {
or
$zoek = $_REQUEST['zoek'];
switch ($_REQUEST['lang']) {
Updated Code
<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<?php
if(!empty($_REQUEST['zoek'])) {
$zoek = $_REQUEST['zoek'];
include 'db.php';
$lang = $_REQUEST['lang'];
if ( empty ( $lang ) ) { $lang = "1"; }
switch ($lang) {
case "1":
echo 'case1';
try
{
$sQuery= "SELECT Medibijsluiter FROM Medispeak WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if($oStmt->rowCount()>0)
{
echo '<table border="2"><thead><td>Medibijsluiter</td></thead>';
while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC))
{
echo '<tr><td>'.$aRow['Medibijsluiter'].'</td></tr>';
}
echo '</table>';
} else {
echo 'Helaas,geen gegevens bekend';
}
break;
}
catch(PDOException $e) {
$sMsg = '<p>
Regelnummer: '.$e->getLine().'<br />
Bestand: '.$e->getFile().'<br />
Foutmelding: '.$e->getMessage().'
</p>';
trigger_error($sMsg);
}
}
$db = null;
}
?>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<h1>Zoeken op Medicijn</h1>
<form method="post" action="" target="Mediframe">
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
Update this code in index file. tabel.php content also added in this
file. So the form will be submitted to the same page and it will not
refer tabel.php.

You're using two different forms, one for language selection and one for searching. Thus, when submitting the second form whatever you selected in the first one gets completely ignored. (Additionally, the first form is using $_GET, and you're looking for the language in $_POST)
A quick fix would be moving your language selection element into the second form, so the selected language gets send along with the search phrase.

change switch ($_POST['lang']) to switch ($_GET['lang']) you mix up POST and GET with your two forms

<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<form method="post" action="tabel.php" target="Mediframe">
<h1>Zoeken op Medicijn</h1>
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
<input id="vlag" type="hidden" name="lang" class="nl" value="<?=$_GET["lang"]?>">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
tabel.php
<html>
<head>
<title>Medispeak</title>
<link rel=stylesheet href=ProjectCSS.css>
</head>
$zoek = $_POST['zoek'];
switch ($_POST['lang']) {
case "1":
try {
$sQuery = "SELECT Medibijsluiter
FROM Medispeak
WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if ($oStmt->rowCount() > 0) {
echo '<table border="2">';
echo '<thead>';
echo '<td>Medibijsluiter</td>';
echo '</thead>';
while ($aRow = $oStmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>' . $aRow['Medibijsluiter'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo 'Helaas,geen gegevens bekend';
}
} catch
(PDOException $e) {
$sMsg = '<p>
Regelnummer: ' . $e->getLine() . '<br />
Bestand: ' . $e->getFile() . '<br />
Foutmelding: ' . $e->getMessage() . '
</p>';
trigger_error($sMsg);
}
$db = null;
break;
}
?>
</body>
</html>
But you can do it more cleaner. you should use a select imput within your second form :)

Related

I would use $ _POST twice in one page

I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>

How to differentiate name in PHP

I have a code. Somehow, it only able to pick the last hidden input name field instead of the other one. I also tried use if and else but nothing is displayed. Please advise.
Without the if else cases scenario:
HTML:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price" value="7.50">
<label for="ldktech_product_good">Good</label>
NOTE: Please include the charger with your iPad trade-in, or a replacement fee will be deducted from the offer
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP:
$condition = $_POST["conditition"];
$price = $_POST["price"];
echo $price;
echo "<br>";
echo $condition;
With the if else scenarios:
HTML Code:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP code:
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];}
echo $price;
echo "<br>";
echo $condition;
Nothing work. Please advised
works fine for me. don't know if your form method is post or not or your action url is set to your script url or if your script is on the same page make sure you place it accordingly.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
</head>
<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" method="post">
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label> <br /><br />
<button id="mysubmit" type="submit">Submit</button><br /><br />
</form>
</div>
<?php
if(isset($_POST["conditition"])){
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];
}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];
}
echo $price;
echo "<br>";
echo $condition;
}
?>
</body>
</html>
I believe this is what you're trying to do?
EDIT:
<form method="post">
<div class="tab-label">
<input type="radio" name="condition1" value="good">
<input type="hidden" name="price1" value="7.50">
<label for="ldktech_product_good">Good</label>
</div>
<div class="tab-label">
<input type="radio" name="condition2" value="flawless">
<input type="hidden" name="price2" value="10">
<label for="ldktech_product_flawless">Flawless</label>
</div>
<input type="submit" name="submit" value="Get records">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['condition1']) && $_POST['condition1'] == "good") {
echo "You have selected :".$_POST['price1']; // Displaying Selected Value
} else if (isset($_POST['condition2']) && $_POST['condition2'] == "flawless") {
echo "You have selected :".$_POST['price2']; // Displaying Selected Value
}
}
?>

trying to add more than one picture onto webpage php

I am trying to add more than one picture to the webpage but it only allows me to add 1. Im trying to add more than 3 images onto the webpage, All ive done is add a button that allows you to select the file but i want more that 1 files and minimum of 4 pictures to be uploaded
heres the code:
<section class="left">
<ul>
<li>Manufacturers</li>
<li>Bikes</li>
</ul>
</section>
<section class="right">
<?php
if (isset($_POST['submit'])) {
$stmt = $pdo->prepare('INSERT INTO bikes (model, description, price, manufacturerId)
VALUES (:model, :description, :price, :manufacturerId)');
$criteria = [
'model' => $_POST['model'],
'description' => $_POST['description'],
'price' => $_POST['price'],
'manufacturerId' => $_POST['manufacturerId']
];
$stmt->execute($criteria);
if ($_FILES['image']['error'] == 0) {
$fileName = $pdo->lastInsertId() . '.jpg';
move_uploaded_file($_FILES['image']['tmp_name'], '../images/bikes/' . $fileName);
}
echo 'Bike added';
}
else {
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
?>
<h2>Add Product</h2>
<form action="addbike.php" method="POST" enctype="multipart/form-data">
<label>Bike Model</label>
<input type="text" name="model" />
<label>Description</label>
<textarea name="description"></textarea>
<label>Condition</label>
<input type="text" name="Condition" />
<label>Price</label>
<input type="text" name="price" />
<label>Category</label>
<select name="manufacturerId">
<?php
$stmt = $pdo->prepare('SELECT * FROM manufacturers');
$stmt->execute();
foreach ($stmt as $row) {
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
?>
</select>
<label>Bike image</label>
<input type="file" name="image" />
<input type="submit" name="submit" value="Add Product" />
</form>
<?php
}
else {
?>
<h2>Log in</h2>
<form action="index.php" method="post">
<label>Username</label>
<input type="text" name="username" />
<label>Password</label>
<input type="password" name="password" />
<input type="submit" name="submit" value="Log In" />
</form>
<?php
}
}
?>
To allow multiple file selection from input file will be like
<input type="file" name="image" multiple>
To fetch all the files selected
print_r($_FILES); // will return you detail of all files in array
The multiple attribute of the input tag is not supported in Internet
Explorer 9 and earlier versions.
You can select multiple files at a time but you need to add multiple then you can select files like this.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="upload.php" method="post" multipart="" enctype="multipart/form-data">
<input type="file" name="img[]" multiple>
<input type="submit">
</form>
</body>
</html>
<?php
echo '<pre>';
$img = $_FILES['img'];
if(!empty($img))
{
$img_desc = reArrayFiles($img);
print_r($img_desc);
foreach($img_desc as $val)
{
$newname = date('YmdHis',time()).mt_rand().'.jpg';
move_uploaded_file($val['tmp_name'],'./uploads/'.$newname);
}
}
function reArrayFiles($file)
{
$file_ary = array();
$file_count = count($file['name']);
$file_key = array_keys($file);
for($i=0;$i<$file_count;$i++)
{
foreach($file_key as $val)
{
$file_ary[$i][$val] = $file[$val][$i];
}
}
return $file_ary;
}

How to increment a value when submit button click

I would like to know how to increase a element, each time that element is posted.
I have to use for loop for the auto increment, but i am not getting right. So any advise or guidance will be great.
Here is the way i have tried to do:
Thanks
<?php
$id=0;
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
$di = count($do);
while ($di > $id) {
$id++;
echo $id;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Try below code, counts are stored in session, but for real life app you should use database and also you should get your products from database:
<?php
// initialize counts for f102 and W122 products
if (!isset($_SESSION['count_f102']) {
$_SESSION['count_f102'] = 0;
}
if (!isset($_SESSION['count_W122']) {
$_SESSION['count_f102'] = 0;
}
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
// increment count for product which was submitted
$_SESSION['count_'.$do] = 1+ (int) $_SESSION['count_'.$do];
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_f102'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_W122'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Are your $_POST['prodCode'] always in the form one letter + id ? If yes maybe this could help :
if (isset($_POST['prodCode'])) {
$value = $_POST['prodCode'];
// save the letter
$letter = substr($value, 0, 1);
// get id
$id = (int) substr($value, 1, strlen($value) - 1);
// get value with letter and incremented id
$valueIncremented = $letter . ++$id;
}
// with $_POST['prodCode'] = 'f102' you will get $valueIncremented = 'f103'
Hope it helps.

PHP Sticky form

When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"

Categories