preg_match not working correctly? - php

I have recently updated one of my php files to allow for a new preg_match but it seems to work only in the first two instances
//Normal Values Check
if(preg_match("/norm/i", $drop) && $ruavalue === "0" || $ruavalue === "2" || $ruavalue === "4" || $ruavalue === "6")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Normal Values Achieved Then Inform User
elseif(preg_match("/norm/i", $drop) && $ruavalue === "1" || $ruavalue === "3" || $ruavalue === "5" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
//Heroic Values Check
elseif(preg_match("/hc/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "4" || $ruavalue === "5")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Heroic Values Achieved Then Inform User
elseif(preg_match("/hc/i", $drop) && $ruavalue === "2" || $ruavalue === "3" || $ruavalue === "6" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
//Mythic Values Check
elseif(preg_match("/myth/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "2" || $ruavalue === "3")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Mythic Values Achieved Then Inform User
elseif(preg_match("/myth/i", $drop) && $ruavalue === "4" || $ruavalue === "5" || $ruavalue === "6" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
The issue im having is that my 1st two button's will display fine but when my $ruavalue = 3 then my 3rd button will not work i dont know if im just being blind to the issue or if its particularly bad code

I think I like leaving those array values as strings, even though they're integers, just because of how my notepad++ colors them with syntax highlighting (text is grey, numbers are red, I like grey). You could do it either way, though.
<?php
$_POST['tier_two'] = 'tier_two';
$drop = 'myth';
$ruavalue = '9';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
// -----------------------------------------
$vals = array('drop'=>$drop,
'ruavalue'=>$ruavalue,
'tier_two'=>$tier_two,
'maintoonqry3'=>$maintoonqry3);
$test = array('norm'=>array('0','2','4','6'),
'hc' =>array('0','1','4','5'),
'myth'=>array('0','1','2','3'));
foreach ($test as $key => $array) {
if (preg_match("/$key/i", $drop)) {
if (in_array($ruavalue,$array)) {
display_my_form($vals);
}
else echo 'You Have RUA\'ed To This Boss';
break; // put this here if you only want to hit
// the first key (norm,hc,myth) match,
// determine valid or not, an d then stop.
}
}
function display_my_form($vals) {
echo "
<form action='' method='post'>
<input name='drop1' type='hidden' value='{$vals['drop']}'>
<input name='ruavalue1' type='hidden' value='{$vals['ruavalue']}'>
<input name='boss' type='hidden' value='{$vals['tier_two']}'>
<input name='main' type='hidden' value='{$vals['maintoonqry3']}'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
";
}
?>
wow....
k, so... i dunno. i tested all regex and good/bad values for it all (both sections of code work). not sure why you have those input fields hidden but that's alright.
<?php
$_POST['tier_two'] = 'tier_two'; // for testing
$drop = 'myth';
$ruavalue = '5';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
$vals = array('drop'=>$drop,'ruavalue'=>$ruavalue,
'tier_two'=>$tier_two,'maintoonqry3'=>$maintoonqry3);
if (preg_match('/norm/i', $drop)) {
if (in_array($ruavalue,array('0','2','4','6'))) {
display_my_form($vals);
}
else echo_error();
}
if (preg_match('/hc/i', $drop)) {
if (in_array($ruavalue,array('0','1','4','5'))) {
display_my_form($vals);
}
else echo_error();
}
if (preg_match('/myth/i', $drop)) {
if (in_array($ruavalue,array('0','1','2','3'))) {
display_my_form($vals);
}
else echo_error();
}
function display_my_form($vals) {?>
<form action='' method='post'>
<input name='drop1' type='hidden' value='<?php echo $vals['drop'] ?>'>
<input name='ruavalue' type='hidden' value='<?php echo $vals['ruavalue'] ?>'>
<input name='boss' type='hidden' value='<?php echo $vals['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $vals['maintoonqry3'] ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
}
function echo_error() {
echo 'You Have RUA\'ed To This Boss';
}
?>
and if you want to keep it your way... this also functions:
<?php
$_POST['tier_two'] = 'tier_two';
$drop = 'myth';
$ruavalue = '5';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
if (preg_match('/norm/i', $drop) && in_array($ruavalue,array('0','2','4','6'))) { ?>
<form action='' method='post'>
<input name="drop1" type='hidden' value='<?php echo $drop ?>'>
<input name="ruavalue1" type='hidden' value='<?php echo $ruavalue ?>'>
<input name="boss" type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name="main" type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match("/norm/i", $drop) && in_array($ruavalue,array('1','3','5','7'))) {
echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('0','1','4','5'))) { ?>
<form action="" method="post">
<input name='drop1' type='hidden' value='<?php echo $drop ?>'>
<input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
<input name='boss' type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('2','3','6','7'))) {
echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/myth/i", $drop) && in_array($ruavalue,array('0','1','2','3'))) { ?>
<form action='' method='post'>
<input name='drop1' type='hidden' value='<?php echo $drop ?>'>
<input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
<input name='boss' type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match('/myth/i', $drop) && in_array($ruavalue,array('4','5','6','7'))) {
echo 'You Have RUA\'ed To This Boss';
}
?>

Related

PHP get request resets page

So, I'm making a PHP page. I'm trying to get a url like this
https://nikkiedev.com/folder/indexfile/?schedule-btn=true&time-zone=CET
but instead, when I click on my GET form for the time zone it goes to
https://nikkiedev.com/folder/indexfile/?time-zone=CET
Here's my code:
<div class='mobile-center'>
<form method="GET">
<button name="schedule-btn" type="submit" value="true" class="btn">schedule</button>
<button name="roles-btn" type="submit" value="true" class="btn">roles</button>
</form>
<?php
$sched_btn = $_GET["schedule-btn"];
$roles_btn = $_GET["roles-btn"];
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}
?>
</div>
Add the value of $sched_btn as a hidden input in the new form.
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<input type='hidden' name='schedule-btn' value='$sched_btn'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}

Form submission doesn't work - PHP

I'm working on a form validation and I'm currently doing some debugging on my code. I'll insert the necessary code snippets below:
Form code:
echo '<h1> Seismic Recording </h1>
<div>
<form action="validate2.php" method="POST">';
echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'required>";
echo 'Wave Type: <select id="wave_type" name="wave_type" required>
<option selected value="s">S</option>
<option value="p">P</option>
<option value="surface_wave">Surface Wave</option>
</select>';
echo "Wave Value: <input type='text' name='wave_value' required>";
echo 'Upload CSV File: <input type="file" name="file">';
echo " <input type='submit' name='submit' value='submit'>";
echo "</form>
</div> ";
Validation code:
echo "w";
if (isset($_POST['submit'])) {
echo "t";
$latitude = $_POST['latitude'];
if ($latitude == NULL) {
echo "Please insert a latitude . <br>";
$error = $error + 1;
}
}
echo "q";
The form validations that I've put within the if statement doesn't work. I tried debugging the code by inserting the 'echo "q"' and 'echo "w"' to see where the problem lies. Those statements work (they output the characters). But the 'echo "t"' within the if statement doesn't work. Why is that so?
Result of var_dump($_POST):
array(5) { ["latitude"]=> string(0) "" ["longitude"]=> string(1) "g"
["wave_type"]=> string(1) "s" ["wave_value"]=> string(1) "g"
["file"]=> string(0) "" }
My issue isn't so much in getting the latitude to validate but why doesn't the 'echo t' work?
Why check for a 'submit' post variable when you're working with latitude?
if(isset($_POST['latitude']))
{
echo "t";
$latitude=$_POST['latitude'];
}
else
{
echo "Please insert a latitude . <br>";
$error = $error + 1;
}
Also there is no need for a name on the submit button
<input type="submit" value="Submit">
You just have your form rendering messed up, this works:
echo "<h1> Seismic Recording </h1>";
echo "<div>";
echo "<form action='' method='POST'>";
echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'd>";
echo 'Wave Type: <select id="wave_type" name="wave_type">';
echo '<option selected value="s">S</option>';
echo '<option value="p">P</option>';
echo '<option value="surface_wave">Surface Wave</option>';
echo "</select>";
echo "Wave Value: <input type='text' name='wave_value' d>";
echo 'Upload CSV File: <input type="file" name="file">';
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";
echo "</div>";
if(isset($_POST)){
if(isset($_POST['submit'])){
echo "Submit is alive!!";
}
}
In order to avoid this kind of issues, remember that you can just write plain html, on this way:
<?php
//Mi php code blablabla
?>
/* My html code */
<h1>Wonderful day, isn't it?<h1>
<?php
//Another php tasks and etc
?>
<!--form code start-->
<?php
if(isset($_REQUEST['req']))
{
echo "Please enter any input";
}
?>
<form action="validate2.php" method="POST">
Latitude: <input type='text' name='latitude'>
<input type='submit' name='submit' value='submit'>
</form>
<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
$latitude = $_POST['latitude'];
if ($latitude == NULL)
{
header("Location:frm.php?req=error");
}
else
{
echo "Your input Text is: ".$latitude;
}
}
?>
<!--Second Method-->
<!--form code start-->
<form action="validate2.php" method="POST">
Latitude: <input type='text' name='latitude'>
<input type='submit' name='submit' value='submit'>
</form>
<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
$latitude = $_POST['latitude'];
if ($latitude == NULL)
{
echo '<script>alert("Please enter any input");
window.location.href="frm.php";</script>';
}
else
{
echo "Your input Text is: ".$latitude;
}
}
?>
Try this:-
$curl_connection =
curl_init('http://www.domainname.com/target_url.php');
link:-http://www.html-form-guide.com/php-form/php-form-submit.html

Getting values from generated inputs

Hello i have one input that generates " text inputs and one submit input" and what is my problem i cant get the value that is written into the generated inputs by user ... here is my code:
<form method="post" action="">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action=''>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='" . $i ."'/>";
}
echo "<input type='submit' name='submit'/>";
echo "</form>";
}
echo $_POST[$i];
?>
I made index.php with the code:
<form method="post" action="index.php?action=post">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if (isset($_GET['action']) && $_GET['action']=='post') {
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action='index.php?action=get_value'>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='somename[]'/><br />";
}
echo "<input type='submit' name='submit' />";
echo "</form>";
}
}
if (isset($_GET['action']) && $_GET['action']=='get_value') {
$somename=$_POST['somename'];
foreach( $somename as $n ) {
print $n;
}
}
?>
All works fine, inputs are generated then values of inputs are received.. All in one index.php file

Php mysql query works in chrome but not firefox/IE

I am just finishing up my university assignment and it is working perfectly in google chrome, unfortunately when I went to test it in firefox and IE there are a few mysql querys that just aren't working. The one below is for adding a song to a database, it does this in Chrome, but when trying to do the same in firefox/IE the page just refreshes and nothing happens. I've tried searching for the past hour but haven't been able to come up with a solution. Any help would be appreciated.
The form and inputs
if (!$edit) {
?>
<form class="inline" method="post" action="dataGridAdmin.php">
<td><input type="text" name="song" size="20"></td>
<td><input type="text" name="artist" size="20"></td>
<td>
<?php
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
?>
<input type="image" src="add.png" name="addTrack" value="yes"></td>
<td><input type="image" src="search.png" name="searchMusic" value="yes"></td>
</form>
<?php
}
?>
</table>
The php and mysql
// do we want to add a new track?
if (isset($_POST["addTrack"]) && $_POST["addTrack"]=="yes") {
$dbQuery="insert into music values (NULL, '".$_POST["song"]."','".$_POST["artist"]."', 'Y')";
$dbResult=mysql_query($dbQuery);
}
FULL FILE:
<html>
<head>
<title>Music Database Editor</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
include "dbConnect.php";
session_start();
if (!(isset($_SESSION["currentUser"]))) header ("Location: adminLogin.php");
$currentUser=$_SESSION["currentUser"];
$currentUserID=$_SESSION["currentUserID"];
$dbQuery="select * from users where id='$currentUserID'";
$dbResult=mysql_query($dbQuery);
$dbRow=mysql_fetch_array($dbResult);
$adminPriv=$dbRow["admin"];
if ($adminPriv=='N') {
header ("Location: adminLogin.php");
}
// print_r($_POST); // this line can be removed after debugging
// set up page size and current page
$pageSize=10;
if (isset($_POST["thisPage"])) $thisPage=$_POST["thisPage"];
else if (isset($_GET["page"])) $thisPage=$_GET["page"];
else $thisPage=1;
// now check for database activity
// do we want to add a new track?
if (isset($_POST["addTrack"]) && $_POST["addTrack"]=="yes") {
$dbQuery="insert into music values (NULL, '".$_POST["song"]."','".$_POST["artist"]."', 'Y')";
$dbResult=mysql_query($dbQuery);
}
// do we want to modify an existing track?
if (isset($_POST["updateData"]) && $_POST["updateData"]=="yes") {
$dbQuery="update music set ".
"song='".$_POST["newSong"]."', ".
"artist='".$_POST["newArtist"]."' ".
"where id=".$_POST["id"];
$dbResult=mysql_query($dbQuery);
}
// do we want to delete a track?
if (isset($_POST["deleteTrack"]) && $_POST["deleteTrack"]=="yes") {
$dbQuery="delete from music where id=".$_POST["id"];
$dbResult=mysql_query($dbQuery);
}
// have we clicked on the edit icon?
if (isset($_POST["editTrack"]) && $_POST["editTrack"]=="yes") {
$edit=true;
$dbQuery="select * from music where id=".$_POST["id"];
$dbResult=mysql_query($dbQuery);
$dbRow=mysql_fetch_array($dbResult);
// set up the values that will appear in the edit form
$editId=$dbRow["id"];
$editSong=$dbRow["song"];
$editArtist=$dbRow["artist"];
}
else $edit=false;
// how many tracks are in the table?
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes")
$dbQuery="select count(id) from music where song like '%".$_POST["song"]."%' and got='Y'";
else
$dbQuery="select count(id) from music where got='Y'";
$dbResult=mysql_query($dbQuery);
$dbRow=mysql_fetch_array($dbResult);
$totalRows=$dbRow[0];
// adjust $thisPage if we have just deleted the only track on the previous page
if (($thisPage*$pageSize)-($pageSize-1)>$totalRows) $thisPage--;
// do we want to search for a track? track name
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
if (isset($_POST["song"]) && $_POST["song"]!="")
$likeStr="where song like '%".$_POST["song"]."%'";
if (isset($_POST["artist"]) && $_POST["artist"]!="")
$likeStr="where artist like '%".$_POST["artist"]."%'";
if (isset($_POST["song"]) && $_POST["song"]!="" && isset($_POST["artist"]) && $_POST["artist"]!="")
$likeStr="where song like '%".$_POST["song"]."%' and artist like '%".$_POST["artist"]."%'";
} else $likeStr="";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") { // are the tracks sorted?
$dbQuery="select * from music $likeStr " .
" order by ".$_POST["sortField"]." ".$_POST["sortDirection"].
" limit $pageSize offset " . ($thisPage-1)*$pageSize;
} else $dbQuery="select * from music $likeStr where got='Y' limit $pageSize offset ".($thisPage-1)*$pageSize;
$dbResult=mysql_query($dbQuery);
$numResults=mysql_num_rows($dbResult);
// which tracks are we currently displaying?
if ($numResults==0) {
$first=0; $last=0;
} else {
$first=(($thisPage-1)*$pageSize)+1;
if ($thisPage<$totalRows/$pageSize) $last=$first+($pageSize-1); else $last=$totalRows;
}
$prevPage=$thisPage-1;
$nextPage=$thisPage+1;
echo "<hr width='1300'>";
echo "<br>";
echo "<h3>Music Database Editor</h3>";
// echo "<p>$dbQuery</p>";
// display button link to previous page
if ($thisPage>1) {
echo "<form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">".
"<input type=\"hidden\" name=\"thisPage\" value=\"$prevPage\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
echo "<input type=\"image\" src=\"previous.png\" alt=\"Previous page\">".
"</form> ";
} else echo "<img src=\"previous.png\"> ";
echo "Displaying tracks $first-$last of $totalRows ";
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes")
echo "containing '".$_POST["song"]."".$_POST["artist"]."' ";
// display button link to next page
if ($thisPage<$totalRows/$pageSize) {
echo "<form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">".
"<input type=\"hidden\" name=\"thisPage\" value=\"$nextPage\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
echo "<input type=\"image\" src=\"next.png\" alt=\"Next page\">".
"</form> ";
} else echo "<img src=\"next.png\"> ";
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
echo "<input type=\"image\" src=\"showAll.png\" alt=\"Show All\">".
"</form> ";
}
?>
<!-- now the current page of tracks -->
<table cellspacing="5">
<tr>
<!-- Sort song name -->
<th><form class="inline" method="post" action="dataGridAdmin.php">
<input type="hidden" name="sort" value="yes">
<input type="hidden" name="sortField" value="song">
<input type="hidden" name="sortDirection" value="asc">
<input type="hidden" name="thisPage" value="<?php echo $thisPage; ?>">
<?php
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
?>
<input type="image" src="sort_ascend.png" alt="Sort A-Z">
</form>
Song
<form class="inline" method="post" action="dataGridAdmin.php">
<input type="hidden" name="sort" value="yes">
<input type="hidden" name="sortField" value="song">
<input type="hidden" name="sortDirection" value="desc">
<input type="hidden" name="thisPage" value="<?php echo $thisPage; ?>">
<?php
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
?>
<input type="image" src="sort_descend.png" alt="Sort Z-A">
</form></th>
<!-- Sort artist name -->
<th><form class="inline" method="post" action="dataGridAdmin.php">
<input type="hidden" name="sort" value="yes">
<input type="hidden" name="sortField" value="artist">
<input type="hidden" name="sortDirection" value="asc">
<input type="hidden" name="thisPage" value="<?php echo $thisPage; ?>">
<?php
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"artist\" value=\"".$_POST["artist"]."\">";
}
?>
<input type="image" src="sort_ascend.png" alt="Sort A-Z">
</form>
Artist
<form class="inline" method="post" action="dataGridAdmin.php">
<input type="hidden" name="sort" value="yes">
<input type="hidden" name="sortField" value="artist">
<input type="hidden" name="sortDirection" value="desc">
<input type="hidden" name="thisPage" value="<?php echo $thisPage; ?>">
<?php
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"artist\" value=\"".$_POST["artist"]."\">";
}
?>
<input type="image" src="sort_descend.png" alt="Sort Z-A">
</form></th><th></th><th></th></tr>
<?php
while ($dbRow=mysql_fetch_array($dbResult)) {
$id=$dbRow["id"];
$song=$dbRow["song"];
$artist=$dbRow["artist"];
// are we editing a track? If so, display the form
if ($edit) {
if ($id==$_POST["id"]) {
echo "<tr><form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">".
"<input type=\"hidden\" name=\"updateData\" value=\"yes\">".
"<input type=\"hidden\" name=\"id\" value=\"$editId\">".
"<td><input type=\"text\" name=\"newSong\" value=\"$editSong\"></td>".
"<td><input type=\"text\" name=\"newArtist\" value=\"$editArtist\"></td>".
" <input type=\"hidden\" name=\"thisPage\" value=\"$thisPage\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
echo "<input type=\"image\" src=\"edit.png\"></td>".
"<td></td></form></tr>";
} else {
echo "<tr><td>$song</td><td>$artist</td><td></td><td></td>";
}
}
// not editing, so display the tracks as text
else {
echo "<tr><td width='300'>$song</td><td width='300'>$artist</td>";
echo "<td><form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">".
" <input type=\"hidden\" name=\"editTrack\" value=\"yes\">".
" <input type=\"hidden\" name=\"id\" value=\"$id\">".
" <input type=\"hidden\" name=\"thisPage\" value=\"$thisPage\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
echo " <input type=\"image\" src=\"edit.png\" alt=\"Edit track\">".
" </form></td>".
"<td><form class=\"inline\" method=\"post\" action=\"dataGridAdmin.php\">".
" <input type=\"hidden\" name=\"deleteTrack\" value=\"yes\">".
" <input type=\"hidden\" name=\"id\" value=\"$id\">".
" <input type=\"hidden\" name=\"thisPage\" value=\"$thisPage\">";
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
if (isset($_POST["searchMusic"]) && $_POST["searchMusic"]=="yes") {
echo "<input type=\"hidden\" name=\"searchMusic\" value=\"yes\">".
"<input type=\"hidden\" name=\"song\" value=\"".$_POST["song"]."\">";
}
echo " <input type=\"image\" src=\"delete.png\" alt=\"Delete track\">".
" </form></td>".
"</tr>";
}
}
// only display the "add track" form if we are NOT currently editing
if (!$edit) {
?>
<tr>
<form class="inline" method="post" action="dataGridAdmin.php">
<td><input type="text" name="song" size="20"></td>
<td><input type="text" name="artist" size="20"></td>
<td>
<?php
if (isset($_POST["sort"]) && $_POST["sort"]=="yes") {
echo "<input type=\"hidden\" name=\"sort\" value=\"yes\">".
"<input type=\"hidden\" name=\"sortField\" value=\"".$_POST["sortField"]."\">".
"<input type=\"hidden\" name=\"sortDirection\" value=\"".$_POST["sortDirection"]."\">";
}
?>
<input type="image" src="add.png" name="addTrack" value="yes"></td>
<td><input type="image" src="search.png" name="searchMusic" value="yes"></td>
</form>
</tr>
<?php
}
?>
</table>
<p></br>&nbsp Logout
</body>
</html>
If it helps, this is what it looks like:
http://i57.tinypic.com/2hpmzbt.jpg
First off, your insert has absolutely no protection against SQL injection. There's a running joke thanks to XKCD about Bobby Tables you can see that explains the whole problem in detail.
Second, I can't tell where the problem is because you're not showing the code that does the output, just the code that does the submission. Is your PHP block on the same page you're submitting to or a separate page? Are you using a redirect?
Try upgrading you php and mysql version. As Abhik Chakraborty said PHP Mysql has nothing to do with the browser !! .
The input type 'image' does not support a value field.
See: http://www.w3.org/TR/html4/interact/forms.html#h-17.4.1
Instead the value of an 'image' input is the coordinates where the user clicked on the image.
Try to check if addTrack.x is set instead:
// do we want to add a new track?
if (isset($_POST["addTrack"]) && isset($_POST["addTrack.x"])) {
$dbQuery="insert into music values (NULL, '".$_POST["song"]."','".$_POST["artist"]."', 'Y')";
$dbResult=mysql_query($dbQuery);
}
As other people states you should also read up on SQL injections.
I see a couple things that could cause issues.
First thing, like the guy before me said you have this open for SQL injection, the least you want to do is filter the $_POST data. Also you have no database provided for your query
<?php
// do we want to add a new track?
if (isset($_POST["addTrack"]) && $_POST["addTrack"]=="yes") {
$db_connection = mysqli_connect("myhost","myuser","mypassw","mydb") or die("Error " . mysqli_error($link));
//Clean the data and get it ready
$addTrack=mysqli_real_escape_string(strip_tags($db_connection,$_POST['addTrack']));
$song=mysqli_real_escape_string($db_connection,strip_tags($_POST['song']));
$artist=mysqli_real_escape_string($db_connection,strip_tags($_POST['artist']));
$dbQuery="insert into music (NULL, '$song','$artist', 'Y')";
$dbResult=mysqli_query($db_connection,$dbQuery);
if($dbResult){
//Your query worked!!
}
}
?>

Storing form data using html & php

I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?
EDITED
I've edited my question to show my code because I'm strugling:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";
Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:
<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />
Or:
<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />
Same goes for option elements inside a select (selected="selected").
Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.
The easiest solution would be adding:
<INPUT type=button value="Cancel" onClick="history.back();">
When you go back, the form would still be there.
You can just use $_REQUEST['Answer'] in value tag of form.

Categories