I have a variable numbr forms I want a user to be able to choose to view via a drop down list. I have assigned each of the form content to array $forms & the associated form name to array $formnames
The idea was to display the variable number of form names as buttons in the drop down selection, and once selected it would display the form content.
I've tried the below code but this leads to the drop down selection immediately showing both the form content as well as name. Could someone point me in the right direction given my limited understanding?
<div class="dropdown">
<button onclick="dropdownfunction()" class="dropbtn">Available List</button>
<div id="avaiablelist" class="dropdown-content">
<?php
$keys = array_keys($forms);
$namekeys = array_keys($formnames);
$arraysize = count($forms);
for($i=0; $i<$arraysize;$i++) { ?>
<?php echo $formnames[$i]['formname']; ?>
<?php
}
?>
</div>
</div>
In this case i would use
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="post">
</form>
to send $forms[$i]['form'] via Post method
Related
I have a slideshow where the user can click a button with an arrow on it pointing to go next and previous. When the user clicks on the arrow I want to save a page name in there, so it will redirect them to the correct page. I also want to store a number so when the user clicks previous or next, the integer will be saved in the correct table userTakingModule, under the checkPoint.
Table layout:
What is the best way to do this i.e. user a button tag inside of a form tag? I have pasted the two ways which I have so far tried to get to happen when the user clicks on one of the two arrows:
a.) Take the user back one page, by having the page1.html in the action of one arrow, and page3.html in the other arrow
b.) Save on the button click, a value into the userTakingModule table.
html attempt 1.)
<div class="arrow-container">
<form style="display: inline" action="dashboard.php" method="post">
<button value="1"><i class="fas fa-arrow-circle-left"></i></button>
</form>
<form style="display: inline" action="slide2.php" method="post">
<button value="3"><i class="fas fa-arrow-circle-right"></i></button>
</form>
</div>
html attempt 2.)
<div>
<form action ="dashboard.php" method = "POST"> <!-- However I may need this page link to change depending on whether they click forward or back -->
<label>Competition Categories</label>
<select name="checkPoint">
<option value="">Select</option>
<option value="1">Previous</option>
<option value="3">Next</option>
</select>
</fieldset>
<button name="save_check_point" type="submit" type="button">View</button>
</form>
</div>
The query I have so far is this:
<?php
// The module ID will always be 5, as they are in the Stress & Anxiety slideshow which has an ID of 5 in the table.
$idUsers = $_SESSION['id'];
$ModuleID = 5;
$checkPoint = $_POST['checkPoint'];
// Preparing and binding my SQL query which inserts a checkpoint number into the table
$stmt = $conn->prepare ("INSERT INTO `userTakingModule` (`userTakingModuleID`, `idUsers`, `ModuleID`, `checkPoint`) VALUES (NULL, ?, ?, ?)");
$stmt->bind_param("iii", $idUsers, $ModuleID, $checkPoint);
$stmt->execute();
?>
I've really been struggling on this for a while now so any help on this would be fantastic, thank you in advance.
I would suggest a simpler aproach. If your pages have the same name format: 'page' followed by the page number, and ending with the file extension (for example page2.html); then you don't need to use forms, since PHP can give you:
The address (including the name of the script) of the current page
The address of the page (if any) which referred the user agent to the current page.
If you don't need a reliable source for this, in security terms, you can use $_SERVER['SCRIPT_NAME'] and $_SERVER['HTTP_REFERER'] to get the info and use simple anchors to link the pages.
This is a PHP code that must be inserted in all slide pages, before your PHP code (that looks good), and before the HTML buttons:
<?php
//A simple function to parse script names and get your page number
function page_number($script_name){
//Remove the first '/' character and the word 'page' to get '#.php'
$number = substr($script_name,1+strlen('page'));
//Remove the extension part to get just the page number
$number = substr($number,0,strpos($number,'.')); //
return $number;
}
//Get the page script name, like '/page#.php'
$script_name = $_SERVER['SCRIPT_NAME'];
$n = page_number($script_name);
//Get the referer URL
$referer = $_SERVER['HTTP_REFERER'];
//Get the script name of that URL
$referer = substr($referer,strrpos($referer,'/'));
$checkPoint = page_number($referer);
?>
When this PHP code ends you have your $checkPoint integer ready to use in your PHP script. And the $n variable to use in the HTML links in the buttons.
This is the HTML for prev and next buttons:
<div class="arrow-container">
<a href="page<?php echo($n-1); ?>.php">
<i class="fas fa-arrow-circle-left"></i>
</a>
<a href="page<?php echo($n+1); ?>.php">
<i class="fas fa-arrow-circle-right"></i>
</a>
</div>
PS: Note the pages need the .php extension to work instead of .html extension.
Page one:
<form action="order.php?pck=1">
<div class="price-btn">
<button href="order.php?pck=1" class="outline-btn">order now</button>
</div></form>
Page two:
<h2>Your Order <?php
if ($_GET[pck]=='1'){
echo 'Package 1';}
?> </h2>
How can i send value from one page to two?
You can pass data tough anchor tag as query string
i.e. order now
Or second way, you can submit form <form action="order.php?pck=1">.
i am totally new on PHP and I have an issue, i hope i can explain my problem exactly.
I have two HTML5 forms where one of the form is being populated by php for loop and inside the same form there is there is a submit button code as follows:
<form class="" name="upgradeChosen" role="form" method="post" action='index.php?action=upgradeChosen'>
<div class="col-md-5">
<select name="upgradeSelected" id="ID_UPGRADE_SELECTED">
<?
foreach ($arrayUpgrade as $value) {
$pro = $value->getnewProduct();
echo '<option value="'.$value->getId().'">'.$pro->getName()." "."===>". $pro->getPrice(Currency::getCurrentCurrency()).'</option>';
}
?>
</select>
</div>
<button style="margin-top: 40px" id="ID_CREATE_ESTIMATE" type="submit" class="btn btn-default"><i class="icon-rocket"></i>Create estimate</button>
</form>
Inside the another PHP file there is a function which is called function upgradeChosen and it parses the selected data from select tag. at the second form i would like to generate a list and populate the selected items from first listbox or select tag. Is this possible ?
Here is my second form code as follows:
<form class ="" name ="upgradeEstimate" role="form" method="post" action='index.php?action=Frontend_upgradeEstimate'>
</form>
and the function which is called upgradeChosen code as follows:
public function upgradeChosen (){
$idUpgradeLine = $this->getRequestAttribute("upgradeSelected");
$upgrade = new UpgradeRates($idUpgradeLine);
$product = $upgrade->getNewProduct();
}
please help me to achieve this if there is a way of doing it only with php i will be happy i do not want to use Jquery, JavaScript or AJAX at first stage if there is a way how to do it with php only.
For this you must use javascript.
You can use onchange event of your select for call a javascript function and reload your page with a post param. Then write your second select in php.
I think there aren't any other method.
Fabio Del Rosso.
Basically in my webpage (fields.php), i have two text boxes(X and Y), one submit button and 2 DIV tags (DIV id="load" , DIV id="result"). Based upon the values we provide in the
text boxes, a query will fetch some values from the database and display it inside the DIV id="load". Also, I have made the results to display as links(I have simply
made the results to display in between anchor tags). So here comes my question, once we click any of the links in the DIV id="load", a query should run in the back ground
and it should display some kind values(fetched from database) inside the DIV id="result". The most important thing i want is, the refresh/back/forward buttons should work normally.
By this i mean, For example, consider we have 4 links like wwww, xxxx, yyyy, zzzz inside DIV id="load" . so if i click on the link wwww, some result (processed by a query) should get
displayed inside the DIV id="result". so now, if i click on xxxx, the result should replace the result for wwww. Also, if i press back button, i should see the result
for wwww inside the DIV id="result".
can some body help me on this? can any one provide the sample code.
Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>history plugin demo</title>
</head>
<body>
<?php
$x = ( !empty( $_REQUEST['X'] ) ? $_REQUEST['X'] : null );
$y = ( !empty( $_REQUEST['Y'] ) ? $_REQUEST['Y'] : null );
?>
Ajax load<BR>
<form id="myForm" action='fields.php' method='GET' rel="history">
X <BR>
<input type="text" name="X" id="ix" value="<?=$x;?>"><BR> <BR>
Y <BR>
<input type="text" name="Y" id="iy" value="<?=$y;?>"> <BR> <BR>
<input id="sub" type="submit" value="Search" align="centre"/>
</form>
<hr>
<!-- here i am performing lot of database queries using php, but to show it to you, i have simplified the code!-->
<div id="load"><a href= "#")> <?php echo $x; ?> </a></div> <!-- so here whne i click on the link, i should pass the link as a text parameter to a query and display the result in div id= result IMPORTANT is : BACK AND REFRESH BUTTON SHOULD WORK AS NORMALLY !-->
<div id="result"> </div>
<hr>
</body>
</html>
As you need to keep the back button to work normally, you can use get parameters to display the results even on clicking back button. I suggest you to change the following lines
<div id="load"><a href= "#")> <?php echo $x; ?> </a></div>
<div id="result"> </div>
to
<div id="load"><a href= "?link=<?php echo $x; ?>")> <?php echo $x; ?> </a></div>
<div id="result">
<?php if (isset($_GET['link'])) {?>
<!--do all the db related stuff here to fetch the link result-->
<?php $link_clicked=$_GET['link'];?>
<!--//this is my stuff-->
<?php
if ($link_clicked == 'link1')
echo "link1 results";
else if ($link_clicked == 'link2')
echo "link2 results";
else
echo "no results";
?>
<?php }?>
</div>
In the result div, the link which was clicked is obtained from the $_GET and the results for the particular link can be fetched from the database.
if you want to clear result div on clicking page refresh button, you can rewrite the browser query string using javascript. Hope this helps :)
Following on from a previous question, (previous question here), the problem I'm having seems to involve trying to pass/post a value through a form when the form action is '#'. I've tried session data but it always returns the last item from the database. Everthing else returns nothing.
Any help/ideas/advice greatly received, S. (Code below)
This is the code that displays the list of items, each containing an 'email' link/button to one instance of a popup window/form that is located at the bottom of the page.
<?php
$query = mysql_query("select * from istable where categoryID = '1'");
while ($result = mysql_fetch_array($query)) {
echo '<h4>'.$result['title'].'</h4>
<p>'.substr($result['descrip'],0,408).'... <strong>Read more</strong></p>
<form action="#" method="post" rel="#sheet" class="see">
<input type="hidden" name="propTitle" value="'.$propResult['title'].'">
<input type="submit" name="submit" value="Email">
</form>
';
}
?>
This is the code for the popup window/form at the bottom of the same page that is called through jquery.
<div id="sheet" class="rounded">
<!--{{{ pane1 -->
<div class="pane" id="pane1">
<h4>Email Details to a Friend</h4>
<p>You have selected to forward the details of <?php echo $_POST['propTitle']; ?> to a friend.</p>
<p>Please fill out the following form</p>
<form class="rounded" id="email-form" method="post" action="<?php echo $pageLink; ?>">
<!-- form goes in here -->
</form>
</div>
<!--}}}-->
</div>
<script type="text/javascript">
$(".see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
$(".error").hide();
});
</script>
Why are you using PHP for this? If the popup is called through the same page, use JavaScript to get the DOM element value and if you need to process data use AJAX.