PHP Value from one page to next page - php

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">.

Related

PHP - dropdown list content showing href link content without selection

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

PHP/HTML form-action attribute

I have a small problem. I have one form, for search. After submitting this form is can't stay on same page.
My url:
www.localhost/index.php?vehicletype=car
On this url, the car search is visible.
After the submit I get this:
localhost/index.php?body_type=any&fuel_type.....
But I want this,
www.localhost/index.php?vehicletype=car?body_type=any&fuel_type....
I tried $_SERVER["REQUEST_URI"] but nothing. Thanks for the help, and sorry for my English grammar!
EDIT: The code:
<? if($_GET['vehicletype']=='car'){ ?>
<div class="search">
<form method="get" action="">
....
<button class="btn btn-search" type="submit">Search <hr> (658 found)</button>
</form>
</div>
First thing following url is wrong
"www.localhost/index.php?vehicletype=car?body_type=any&fuel_type...."
after "?" it will set all argument in get method you can access that variable using $_GET['variablename']
if you want to be on same page you can action like following way
<form action="#" method="GET">
When someone click on submit button same page will be called with form params.
You can get all form param by using $_GET method.
Method 1.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
else
{
?>
Show HTML Form here
<?php } ?>
Method 2.
<?php
if(isset($_GET['vehicletype']) && $_GET['vehicletype'] == 'car')
{
//write business login that you want to show once search action perform by user
}
<form action="#" method="GET">
<input type="text" name="vehicletype" value="<?php if (isset($_GET['vehicletype'])) { echo $_GET['vehicaletype']; } ?>">
</form>
Let me know if you have further query.

HTML Button that saves and move to the next page

I have a small problem. I want to have a button in my html page that saves every data that is added in the textfields and also when I click it to move to the next page.
My code is the follow...
<input type=button onClick="location.href='education.php'" value='Next'>
but it only moves to next page it does not save the data in the database ...
Can you help me please?
Thanks.
Remove the JavaScript
Change the type to submit
Wrap it in a <form>
Set the action of the form to education.php
Set the method of the form to post
Then, in education.php, read the data from $_POST and use PDO (with bound variables) to insert it into the database.
Try this :
<?php
if(isset($_POST['submit']))
{
// Insert Query Put here
header('Location: education.php');
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Next" name="submit">
</form>
</body>
</html>
education.php :
<?php
echo "Successfully Updated.";
?>
You will have to set an action to your form like below because you are not submitting the form, but just redirection to another page without taking the form data.
<form action="education.php" method="post">
<!-- All your input fields here -->
<input type="submit" name="submit" value="Next">
</form>
and your education.php should be look like this:
<?php
//Get all parameters using $_POST
//Make A connection to database
//Choose a database in which you have to save the data
//Create a SQL query
// run query using mysql_query($query);
//Redirect to anywhere with header("Location:page.php");
?>

clicking on a link should display the result in same page .BACK and REFRESH button have to work normally

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 :)

PHP Pass variable to popup form within same page

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.

Categories