Including a html form in a php script - php

I am trying to create a PHP script which has a HTML form included within it. Please help me edit this:
$SoapClient = new SoapClient ( NULL, $options );
try {
$cities = $SoapClient->getCities ();
// echo var_dump($SoapClient->getCities ());
echo "<form action= " . "'$" . "PHP_SELF' method = 'post'>";
echo "<h4>Choose Origin</h4>";
foreach ( $cities as $city ) {
echo "<input type='radio' name = 'origin' value = " . $city . "> <br>";
}
echo "<h4>Choose Destination</h4>";
foreach ( $cities as $city ) {
echo "<input type='radio' name = 'destination' value = " . $city . "> <br>";
}
echo "<p>";
echo "<input type='submit' name='submitButton' value='Calculate Great Circle'>";
// show soap request and response
} catch ( Exception $e ) {
echo "<h3>SOAP error</h3><pre>" . $e . "</pre>";
echo "<h3>SOAP error last response</h3><pre>" . $SoapClient->__getLastResponse () . "</pre>";
echo "<h3>SOAP error last request</h3><pre>" . $SoapClient->__getLastRequest () . "</pre>";
}
Thanks a lot :)

You want to display the cities names next to your radios, try this in your two loops :
foreach ( $cities as $city ) {
echo "<label><input type='radio' name='origin' value='" . $city . "'>" . $city . "</label> <br>";
}
And also replace
echo "<form action= " . "'$" . "PHP_SELF' method = 'post'>";
by
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';

Related

use a foreach to output data from a database

I've a very simple database, a list of 8 languages and the values they can have is Y or N.
What I need is to output a checkbox for each languages and, if the value is Y, the checkbox must be checked, otherwise it must be empty.
This is the code I'm using for each single language, but I would like to know if there's a better way to obtain the same result.
if ($language=='N'){
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . "'> English<br>";
} else {
echo "<input type='checkbox' name='" . $rowlang["english"] . "' value='" . $rowlang["english"] . " checked'> English<br>";}
//What I've tried to do is to build an array of the languages and use a foreach
$languages = array($rowlang["czech"],$rowlang["english"],$rowlang["german"],$rowlang["slovak"],$rowlang["russian"],$rowlang["french"],$rowlang["spanish"],$rowlang["italian"]);
foreach($languages as $language)
if ($language=='N'){
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'> " . $rowlang . "<br>";
}else{
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "' checked> " . $rowlang . "<br>";
}
which is almost working, the problem is that I can't echo the single language, because with this code I'm getting "array".
The problem is that trying to use $rowlang as part of the echo is a problem as it is an array.
Instead you can create an array of the languages you want to output, this also gives the name to be displayed as the value. So use a foreach() over this array and check in the $rowlang array to see if it is set. Rather than repeat the whole HTML, this just sets the checked attribute.
// Need to expand this array for all the countries you need
$languages = array("german" => "Germany","english" => "English");
foreach($languages as $language => $label) {
if ($rowlang[$language]=='N'){
$checked = '';
}else{
$checked = ' checked';
}
echo "<input type='checkbox' name='" . $language . "' value='" . $language . "'$checked>" . $label . "<br>";
}
This will output something like
<input type='checkbox' name='german' value='german'>Germany<br>
<input type='checkbox' name='english' value='english' checked>English<br>
First create an associative array and store the languages and their values in it:
$languages = array('czech' => 'N','english' => 'Y','german' => 'N','slovak' => 'N','russian' => 'N','french' => 'N','spanish' => 'Y','italian' => 'N' );
then:
foreach($languages as $key => $value) {
if ($value=='N'){
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "'> " . $key . "<br>";
}
else{
echo "<input type='checkbox' name='" . $key . "' value='" . $value . "' checked> " . $key . "<br>";
}
}
the result will be:
if it is array you should use $language->language_name or $langage['language_name'] as of your array type

php If statement as part returned value inside of a function

I have a function to see what a user's role is. It works great on it's own in the html document. But I have several functions that connect to a database and print information. I want certain parts of the function to be available to all users and some only to admin. Right now it is printing <?php ?> in the source file.
function isUserInRole($userRole){
$retVal = false;
if ($userRole == $_SESSION['role']) {
$retVal = true;
}
return $retVal;
}
This works fine in the html document:
<?php if(isUserInRole('Admin')){ ?>
<?php print "<a href='edit/staffDetailsForm.php?ID=$staffId'><button>Edit ></button></a>" ?>
<?php } ?>
Here is a function that is not working:
function getLabelCodes ($staffId, $staffName, $compId){
$retVal = "";
include "inc/DBconnect.php";
$query = "SELECT l.listName, l.listCode FROM labels AS l INNER JOIN agtLabels AS al ON l.listCode = al.listCode WHERE al.staffId = $staffId AND al.compId = $compId";
if ($result = $mysql->query($query)) {
while ($aRow = $result->fetch_assoc()) {
$listCode = $aRow['listCode'];
$retVal = $retVal . "<li class='remove'><form action='delete/removeStaffLabel.php' method='post' onsubmit='return confirm('Do you really want to remove" . $staffName . "from " . $listCode . "?')'>";
$retVal = $retVal . $aRow['listName'] .
" <input type='hidden' name='staffId' value='" . $staffId . "' />
<input type='hidden' name='compId' value='" . $compId . "' />
<input type='hidden' name='listCode' value='". $listCode . "' />
<input class='remove' type='submit' value='Remove from " . $aRow['listName'] . "' />
</form>
</li>";
}
} return "<?php if(isUserInRole('Admin')){ ?><h3>Label Lists:</h3><a href='edit/staffLabels.php?staffId=" . $staffId . "&compId=" . $compId . "' /><button>Add to Label list</button></a><ul>" . $retVal . "</ul><?php } ?>";
$mysqli->close();
}
I have tried placing the<?php if(isUserInRole('Admin')){ ?> bit in the return (as it is now) as well as part of $retVal. I have to find a way to fix it within the function because I have many other functions that are similar and I have no real way of breaking them up further.
The source-code prints </contact><comments></comments><?php if(isUserInRole('Admin')) { ?><h3>Label Lists:</h3>. How do I get isUserInRole() to perform it's action prior to the page load being complete?
Any help would be greatly appreciated.
You can do it inside your function:
if(isUserInRole('Admin')){
return "<h3>Label Lists:</h3><a href='edit/staffLabels.php?staffId=" . $staffId . "&compId=" . $compId . "' /><button>Add to Label list</button></a><ul>" . $retVal . "</ul>"
}

PHP mulitiple checkboxes

I am stuck on a script and need another pair of eyes to see if I am missing something. The script is for a bookshop. When a student number is in-putted and searched for the student is displayed with the books that he is suppose to get for each subject. The student, course and book data comes from a MySQL database.
This is all done with this script:
<?php
if (isset($_POST['submit'])){
$btnClick = $_POST['submit'];
switch($btnClick){
case "Logout" :
session_destroy();
header("location:index.php");
break;
case "Search" :
$Validate = $_POST['txtStud'] ;
$StudNr = ValidateTxt($Validate);
$showStud = findStud($StudNr);
$cid = $showStud[4];
$showBooks = findBooks($cid);
echo "<form action='issue_book.php' method='post'>";;
echo "<table class='table3'>";
echo "<tr>";
echo "<td>" . $showStud[0] . " " . $showStud[1] . " " . $showStud[2] ."</td>";
echo "</tr>";
echo "<tr><td></td><td>" . $showStud[3] . "</td></tr>";
$array_count = count($showBooks);
$num = 0;
while ($num != $array_count) {
$bookNum = $showBooks[$num]['bid'];
echo $bookNum . "<br>";
echo "<tr><td>" . $showBooks[$num]['bid'] . "</td>" . "<td>" . $showBooks[$num]['bname'] . "</td>" ;
echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
$num++;
}
echo "</table>";
echo "<br>";
echo "<table class = 'table3'>";
echo "<tr><td></td><td><input type='submit' name='submit' value='Issue'></td>
<td><input type='submit' name='submit' value='Clear'></td></tr>";
echo "</form>";
break;
case "Issue":
$mybooks = $_POST['booknum'];
$h = count($mybooks);
echo $h . "<br>";
print_r ($mybooks);
break;
}
}
?>
At the bottom of the dynamic created data there is 2 buttons. When I click on the Issue button I am presented with this data.
This comes from the code as it is in the script at this moment. I want to send the data from here to the database.
Array ( [0] => [1] => [2] => )
An empty array?? Not sure what happened to the names that I assigned each check box??
I tried to adapt my script according to this forum post Check box link
I am not sure where I am missing something.
This is because you have a syntax error here
echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
^php tags are opened ^
You are already printing your table inside php tags, you cannot open other tags
value='<?php echo $bookNum; ?>
This is why your array's values are empty but keys exists. You just need to concatenate
echo "<td><input type='checkbox' name='booknum[]' value='".$bookNum."'></td></tr>";

Variable at POST

In this code you can see arraying data from MySQL database and preparing them to change.
But I have a problem with a variables $_POST[$here]. How do I can insert the variable to that place. Read the code for better understanding. This code does not work. Are there some other ways. Or that is impossible to do something like this thing. If I do not use variables that changing me every or no one value. I am crying right now, that is so frustrating problem. Thanks for every answer...
while ($row = mysql_fetch_array($result) or die(mysql_error())){
$link = $row['link'];
$tittle = $row['tittle'];
$content= $row['content'];
echo "<div>";
echo "<form name='"; echo $link; echo "' method='POST' action='login.php'>";
echo "<h1>"; echo $link; echo "</h1>";
echo "<h3>"; echo $tittle; echo "</h3>";
echo "<input type='text' name='"; echo $link; echo "tittle'>";
echo "<h3>"; echo $content; echo "</h3>";
echo "<textarea name='"; echo $link; echo "content'></textarea>";
echo "<input type='submit' name='"; echo $link; echo "' value='change'>";
echo "</form>";
echo "</div>";
$var1 = $link."tittle"; $titt = $_POST[$var1];
$var2 = $link."content"; $ten = $_POST[$var2];
mysql_query("UPDATE inbox SET tittle='".$titt."', content='".$ten."' WHERE link='".$link."'");
echo $link;
}
I fixed that!!!
There is a lot of wrong with your code:
You use mysql_* functions instead of PDO
You don't escape your $_POST value exposing your website for any hacker to hack into. At least use mysql_real_escape_string(), but see point 1
You define $titt and then use $tit, which causes an error
You keep echoing instead of simply concatenating a string and then echoing that string
There's a lot to fix...
Excluding the security errors, you can try this:
while ($row = mysql_fetch_array($result) or die(mysql_error())){
$link = $row['link'];
$tittle = $row['tittle'];
$content= $row['content'];
echo "<div>";
echo "<form name='" . $link . "' method='POST'>";
echo "<h1>" . $link . "</h1>";
echo "<h3>" . $tittle . "</h3>";
echo "<input type='text' name='" . $link . "tittle'>";
echo "<h3>" . $content . "</h3>";
echo "<textarea name='" . $link . "content'></textarea>";
echo "<input type='submit' name='" . $link . "' value='change'>";
echo "</form>";
echo "</div>";
$var1 = $link."tittle";
$var2 = $link."content";
if (isset($_POST[$var1]) and isset($_POST[$var2])) {
$titt = $_POST[$var1];
$ten = $_POST[$var2];
mysql_query("UPDATE inbox SET tittle='".$titt."', content='".$ten."' WHERE link='".$link."'");
}
echo $link;
}

id of the input

I'm sending muliple inputs with form, How can I get the id of the actuall input, so i can update mysql content?
my code:
if (isset($_POST['save']))
{
foreach ($_POST as $key => $value)
{
${$key} = $value;
mysql_query("UPDATE table SET img='$image_[id]', title='$title_[id]' where id='$id'");
}
}
while ($row = mysql_fetch_array($result))
{
echo "<b>Title: </b><input type='text' name='title_" . $row['id'] . "' value='" . $row['title'] . "'><br />";
echo "<b>Image: </b><input type='text' name='image_" . $row['id'] . "' value='";
}
echo '<input type="submit" name="save" value="save"><br /><hr>';
echo '</form>';
Yours naming is wrong. Use this way.
echo "... name='title[" . $row['id'] . "]'...";
And after it your $_POST['title'] will contain array of titles.
And read manual How do I create arrays in a HTML ?
But if you are still what to go that way. You can use something like this.
foreach ($_POST as $key => $value)
{
if (preg_match('^image_(\d*)$', $key, $matches))
{
$id = $matches[1];
if (isset($_POST['title_' . $id]))
{
mysql_query("UPDATE table SET title='" . $_POST['title_' . $id] . "' where id='$id'");
}
}
}
But sign here that you understand all vulnerabilities of this code.

Categories