How to make the php strings selectable for the html query? - php

I'm looking to make the the suggestions given by the Dictionary API to become links that can be queried or that are inserted directly in to the text are field before they are searched. I'm looking to achieve some kind of query expansion in any case.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search Attempt</title>
</head>
<body>
<form method="POST" action='AllinOneMonstaaa.php'>
<label for="query">Query</label><br/>
<input name="query" type="text" size="60" maxlength="60" value="" /><br /><br />
<select name ="agg">
<option value="Aggregated">Aggregated</option>
<option value="Non-Aggregated">Non-Aggregated</option>
<option value="Bing">Bing</option>
<option value="Blekko">Blekko</option>
<option value="Faroo">Faroo</option>
</select>
<input name="bt_search" type="submit" value="Search" /> </form>
<h2> Results </h2>
</body>
</html>
<?php
if ($_POST['query'])
{
$query = urlencode($_POST['query']);
$s_count = 0;
$ss_count = 0;
$query = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/'.$query.'?
$xml = new SimpleXMLIterator(file_get_contents($query));
foreach ($xml -> suggestion as $suggestion[$s_count])
{
$s_count++;
}
if ($s_count > 1)
{
echo ('<h4>Did you mean?</h4>');
while ($ss_count <=$s_count)
{
echo ($suggestion[$ss_count].'<br>');
$ss_count++;
}
}
}

'Quick bug, there's a missing ' on the $query line.
echo '<a href="whatever">'. $suggestion[$ss_count].'<br>';
Edit: This will send them to Dictionary.com with the word to look up there. Outside that I have no idea how else you would formulate the url.
echo '<a href="http://dictionary.reference.com/browse/"' . $suggestion[$ss_count] . '" target="_new">' . $suggestion[$ss_count] . '<br>';

Related

Getting radio input from selection in table

I'm trying to get my code to open a page that displays a picture of the model and displays information. the code uses a csv file that populates the table. I cant seem to figure out how to get my radio button to pass data based on selection. I'm trying to find the selected row and display the data on another page basically. any help would be appreciated. Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Choose One</h1>
<form action="index.php" method="post">
<label for="Type">Package Type</label>
<select name="Type">
<option value="">Choose...</option>
<option value="a">apple</option>
<option value="b">google</option>
<option value="c">oneplus</option>
<option value="d">Samsung</option>
</select>
<input type="Submit" >
</form>
<?php
echo "<html><body><table border = '1'>\n\n";
echo "<form action='stock.php' method='post' name='stock'>";
echo "<th>Item</th><th>Manufacturer</th><th>Model</th><th>Price</th>";
$myfile = fopen("phones.csv", "r");
$ind=0;
$ind++;
while (($dataarray = fgetcsv($myfile)) !== false)
{
echo "<tr><td>"."<input type = 'radio' name='manufacturer' id=manufacturer
value=$dataarray[$ind]>"."</td>";
foreach ($dataarray as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$ind = 0;
}
fclose($myfile);
echo "\n</table>
<input type='submit'>
</form></body></html>";
$ind = 0;
?>
</body>
</html>
Start of stock.php
<!DOCTYPE html>
<html lang="en">
<head>
<form action="index.php" method="get" name="stock">
</form>
<php?
if(isset($_get[manufacturer]))
{
$value=$_get[manufacturer];
}
?>
</head>
<body>
</body>
</html>
This is a snip of the csv file:

Cant fetch variable after submitting the form

The variable $role_id1 is not being fetched in $role_id in $_POST['add sub menu'].
I want to store $role_id1 in $role_id and insert into database.Afer i click the submit button the role_id1 is fetching the parent menu but after i click add sub menu the role_id is storing 0 at backend.But i want it to store the vale of role_id1 which is being fetched after i click submit.Suggest any solution if possible.
<?php
$dbcon = new MySQLi("localhost","root","","menu");
if(isset($_POST['add_main_menu']))
{
$menu_name = $_POST['menu_name'];
$parent_id = 0;
$role_id = $_POST['role_id'];
$menu_link = $_POST['mn_link'];
$sql=$dbcon->query("INSERT INTO menu VALUES('','$menu_name','$parent_id','$role_id','$menu_link')");
}
if(isset($_POST['add_sub_menu']))
{
$parent_id = $_POST['parent'];
$name = $_POST['sub_menu_name'];
$role_id = $role_id1;
$menu_link = $_POST['sub_menu_link'];
$sql=$dbcon->query("INSERT INTO menu VALUES('','$name','$parent_id','$role_id','$menu_link')");
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dynamic Dropdown Menu</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
</head>
<body>
<div id="head">
<div class="wrap"><br />
<h1>Back to menu</h1>
</div>
</div>
<center>
<pre>
<form method="post">
<input type="text" placeholder="menu name :" name="menu name" /><br />
<input type="text" placeholder="role id :" name="role_id" /><br />
<input type="text" placeholder="menu link :" name="mn_link" /><br />
<button type="submit" name="add_main_menu">Add main menu</button>
</form>
</pre>
<br />
<pre>
<form method="post">
<select name="role_id">
<option selected="selected">select role id</option>
<?php
$res=$dbcon->query("SELECT distinct role_id FROM menu");
while($row=$res->fetch_array())
{
?>
<option value="<?php echo $row['role_id']; ?>"><?php echo $row['role_id']; ?></option>
<?php
}
?>
</select><br />
<input type="submit" value="submit" name="submit">
<?php if(isset($_POST['submit']))
{
?>
<select name="parent">
<option selected="selected">select parent menu</option>
<?php
$role_id1 = $_POST['role_id'];
$res=$dbcon->query("SELECT * FROM menu where role_id= $role_id1 AND parent_id=0 ");
while($row=$res->fetch_array())
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']
;
?></option>
<?php
}
}
?>
</select><br />
<input type="text" placeholder="menu name :" name="sub_menu_name" /><br />
<input type="text" placeholder="menu link :" name="sub_menu_link" /><br />
<button type="submit" name="add_sub_menu">Add sub menu</button>
</form>
</pre>
back to main page
</center>
</body>
</html>
$role_id1 = $_POST['role_id'];
The lifetime of $role_id1 is where your scripts stops (last line) and output is sent to the browser (you see the form again).
So on the line in the top of your code:
$role_id = $role_id1;
$role_id1 doesn't exists anymore. If you view your error-log (or turn on display_errors), you would see a Notice: Undefined variable: role_id1 in ...
If you want to keep that value, put it in a hidden element so that it will be included in the POST-data the next time you submit that form:
echo '<input type="hidden" name="previous_role_id" value="' . htmlspecialchars($_POST['role_id']) . '">';
One sidenote (for completeness), although the element is hidden the value can be altered by the user.

Display database values on option

I am trying to display the saved data in the database on select before a user can update other choice. I am not really sure how to code it. Can anyone come out with possible solution? Had tried many ways but unable to do so.
Update: I have found out the problem why the options does not show.
This is my code:
`
$consentId = $_GET['consent_id'];
$retrieveConsent = "SELECT * FROM consent, leavetype WHERE consent.type_of_leave = leavetype.type_of_leave";
$retrieveResult = mysqli_query($link, $retrieveConsent) or die("Retrieve Error" . mysqli_error($link));
$queryleavetype = "SELECT * FROM leavetype";
$queryleaveresult = mysqli_query($link, $queryleavetype) or die("Leave Retrieve Error " . mysqli_error($link));
$row = mysqli_fetch_array($retrieveResult);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Consent </title>
</head>
<body>
<div class="ui-content">
<h3><b>Edit Leave</b></h3>
<form action="doEditConsent.php" method="post" data-ajax="false">
<input type="hidden" name="cId" value="<?php echo $row['consent_id']; ?>"/>
<label for="dateFrom" ><b>Date From:</b></label>
<input type="date" id="dateFrom" name="newDateFrom" value="<?php echo $row['consent_date_from']; ?>" required>
<br>
<label for="dateTo" ><b>Date To:</b></label>
<input type="date" id="dateTo" name="newDateTo" value="<?php echo $row['consent_date_to']; ?>" required>
<br>
<label for="reason" ><b>Leave Type:</b></label>
<select name="leaveType" id="leaveType" data-mini="true">
<?php
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
?>
<option value="<?php echo $rowleave['type_of_leave']; ?>">
<?php echo $rowleave['leave_type']; ?>
</option>
<?php
};
?>
</select>
<br>
<button class="ui-btn ui-corner-all" type="submit" >Submit</button>
</form>
</div>
<?php
}
}
?>
</body>
</html>`
Try this, hopefully it will help you:
<select name="leaveType" id="leaveType" data-mini="true">
<option value=""></option>
<?php
$previous_selected_value = 'previous_selected_value';//get the previous value and assign it to this variable
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
$selected = ($rowleave['type_of_leave'] == $previous_selected_value) ? " selected='selected'" : "";
echo '<option value="'.$rowleave['type_of_leave'].'"'.$selected.'>'.$rowleave['leave_type'].'</option>';
}
?>
</select>

What is wrong with this php search page?

I found a tutorial that looked like it would do what I've been trying to do without success. I adapted it to my details and tried it. It doesn't work. When you enter the search and hit submit, all it does is go back to the beginning. I can't see anything wrong with the code so after a couple of hours of trying things, here it is. Can you see what is wrong?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>test</title>
</head>
<body>
<?
if ($searching =="yes")
{
echo "<h2>Search</h2><p>";
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
mysql_connect('localhost', 'user', 'password') or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM engravers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['Country'];
echo "<br>";
echo $result['Year'];
echo "<br>";
echo $result['Engraver1Surname'];
echo "<br>";
echo $result['Designer1Surname'];
echo "<br>";
echo $result['Printer'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
echo "<b>Searched For:</b> " .$find;
}
?>
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Search for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="Country">Country</option>
<Option VALUE="Year">Year</option>
<Option VALUE="Engraver1Surname">Engraver</option>
<Option VALUE="Designer1Surname ">Designer</option>
<Option VALUE="Printer">Printer</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
</body>
</html>
As mentioned in my comment, after POSTing, you need to grab the variables from the $_POST array. Something like:
if ($_POST['searching'] == "yes") {
$find = $_POST['find'];
$field = $_POST['field'];
// etc...
This looks like very old PHP code that had register_globals on. It doesn't work like that anymore.
Use the superglobal $_POST to get to your variables, for example:
if ($_POST['searching'] =="yes") {
...
}
Also, read into SQL injection and how to avoid it.
You need to set the variables on begin:
//set default values
$find="";
$searching="";
$field="";
If(isset($_POST['searching']) && $_POST['searching']="yes"){
$find= mysql_real_escape_string($_POST['find']);
$searching=mysql_real_escape_string($_POST['searching']);
$field=mysql_real_escape_string($_POST['field']);
...

In PHP how to output the contents of a radiobox array using POST?

I want to output the values of the checkbox in the same way I'm outputting the values from the text fields. Since the checkbox can have multiple inputs I'm using an array for that but trying to cycle through the array for values hasn't worked for me.
tried foreach($type as $item) and echoing $item within the HTML like it says in the PHP book I have but that hasn't worked.
How should I do and where should the code be? I'm also unable to use PHP within the HTML for some reason, I'm not sure why that is or if its something to do with the echo<<<_END or not. Help appreciated.
<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre'];
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = $_POST['type'];
else $type = "(Not entered)";
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: $game in the $genre genre and of the type<br />
<form method="post" action="formtest.php">
What is your game?
<input type="text" name="game" />
<br />
What is your genre?
<input type="text" name="genre" />
<br />
Type?
Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
<input type="submit" />
</form>
</body>
</html>
_END;
?>
With the form as it is now, $_POST['type'] will be an array since it's using checkboxes (and named appropriately), not radios. Here I just implode it for display, but you can loop through it like any array. It should be worth noting that any time you're wondering what a form is giving you, you can var_dump($_POST) or var_dump($_GET) depending on where the data is coming from. It helps a lot with debugging.
Here's what I got, I switched from heredoc, but your heredoc should work fine if you add $type back in somewhere, I didn't notice it in the original code:
<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre']; //Edit: Fixed line, oops
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = implode(', ',$_POST['type']);
else $type = "(Not entered)";
//Normally I'd specify a charset, but for simplicity's sake I won't here.
$type = htmlspecialchars($type);
$game = htmlspecialchars($game);
$genre = htmlspecialchars($genre);
?>
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: <?php echo $game; ?> in
the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
<form method="post" action="">
What is your game?
<input type="text" name="game" />
<br />
What is your genre?
<input type="text" name="genre" />
<br />
Type?
Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
<input type="submit" />
</form>
</body>
</html>
Addendum:
If you switched and used radios like
<input type="radio" name="type" value="Downloadable" />
$_POST['type'] would be a simple string since you can only select one of the set.
To the file you post it the type[] will be saved as an array. For example
$a=$_POST['type'];
Although I don't find any point in doing this to radio-buttons, because their purpose is to pass only 1 value(unless you want specifically to).
Ok, first you don't need to echo the entire html output. Second your questions says radio buttons, but the html shows checkboxes. A radio field will only produce one result so you don't need [] after then name. Checkboxes will return an array when named with a []. So if you are using checkboxes you will need to process the result as an array. If you change the field to radio it should work fine.
<?php // formtest.php
if (isset($_POST['game'])) {
$game = $_POST['game'];
}
else { $game = "(Not entered)"; }
if (isset($_POST['genre'])) {
$genre = $_POST['genre'];
}
else { $genre = "(Not entered)"; }
if (isset($_POST['type'])) {
$type = $_POST['type'];
}
else { $type = "(Not entered)"; }
?>
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: <?php echo $game; ?> in the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
<form method="post" action="test.php">
What is your game?
<input type="text" name="game" <?php if ($game != "(Not entered)") { echo "value='" . $game . "'"; } ?> />
<br />
What is your genre?
<input type="text" name="genre" <?php if ($genre != "(Not entered)") { echo "value='" . $genre . "'"; } ?> />
<br />
Type?
Retail <input type="radio" name="type" value="Retail" <?php if ($type == "Retail") { echo "checked"; } ?> />
Downloadable <input type="radio" name="type" value="Downloadable" <?php if ($type == "Downloadable") { echo "checked"; } ?> />
Free <input type="radio" name="type" value="Free" <?php if ($type == "Free") { echo "checked"; } ?> />
<br />
<input type="submit" />
</form>
</body>
</html>

Categories