I am learning PHP and have a situation where I want the method to be POST to pass variables to my php and
1) Connect to Server2) store results in a variable3) Display an HTML Table
However, I later on in my syntax want to use GET to "recall" that variable and output the results to a CSV file. This is what I have, but when I click the button Generate nothing happens. Is this possible? If not, how should I re-write the syntax to make ithappen?
<body>
<form method="POST">
End Date:<input type="date" name="end" value="<?= $_POST['end'] ?>">
<input type="submit" name="submit" value="Go">
</form>
</body>
<?php
if (isset($_POST['submit']))
{
//Connect To Server
//Store Result Set in Variable
//Display HTML Table
}
?>
<form method="get">
<button type="submit" name="csv" value="1">Generate CSV</button>
</form>
Related
I have a html page with a few buttons which I would like to take me to another PHP page, this page will handle all logic but I'm having trouble setting sessions.
I could be going about this the wrong way so i'd appreciate if someone pointed me in the right direction.
This is the HTML page code I'm working with:
<form action="reportPage.php">
<center><input type="submit" value="View customers"></center>
<?php
$_SESSION['customer'] = "checkCusTrains";
?>
</form>
<form action="reportPage.php" mehtod="post">
<center><input type="submit" value="View admins"></center>
<?php
$_SESSION['admins'] = "checkCusAdmin";
?>
</form>
I would like each button to go to the reportUser.php page, but with a different session, as I have if/else statements set up in the report page that will display the information corresponding to that session.
How can I achieve this? As it stands, both session variables are being set
I suggest you use query but not session to achieve this:
<form action="reportPage.php?customer=checkCusTrains">
<center><input type="submit" value="View customers"></center>
</form>
<form action="reportPage.php?admins=checkCusAdmin" mehtod="post">
<center><input type="submit" value="View admins"></center>
</form>
And in your reportPage.php:
<?php
if(!empty($_GET['customer']) && $_GET['customer'] == 'checkCusTrains'){
//do something
}
if(!empty($_GET['admins']) && $_GET['admins'] == 'checkCusAdmin'){
//do something else
}
?>
Include a variable in your form that you can check server-side:
<form action="reportPage.php" method="post">
<input type="hidden" name="session" value="checkCusAdmin">
<input type="submit" ...>
</form>
In this way, each form can contribute a special field variable which is hidden to the user, but usable to the server. It can identify what form was submitted or what action to take with the data.
In your case, the "hidden" field might contain the name of the session.
You need to add hidden field in your form that will hold the value of your sessions, if the values of your sessions are already given like 'checkCusTrains' and 'checkCusAdmin', you don't need to use session, just put the value in the hidden input box.
<form action="reportPage.php">
<input type="hidden" name="session" value="checkCusTrains">
<center><input type="submit" value="Check customers"></center>
</form>
<form action="reportPage.php">
<input type="hidden" name="session" value="checkCusAdmin">
<center><input type="submit" value="Check admins"></center>
</form>
And in your reportPage.php
you can get the value by $_POST['session']; , then do whatever you want using if else statement that you have.
I advise you to either use a query for this or to use jQuery cookie and bind the cookie set to the button click action. The code you posted does not do what you think. In that code both cookies are set when the page loads and that is when the PHP sets the cookies.
My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];
<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query
I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.
Background: The website I am working on has a search bar at the top. The user inputs a part code for a product and the website returns information about that product.
So, I have a basic search bar that posts parameters from a HTML form into a PHP script, which then does a lookup on a MySQL server to get the Product.
The problem is because some part codes have "#" characters, I have to use Javascript to insert escape characters, otherwise I only get some of the part code in the PHP script.
Example - 123#ABC would be read as 123.
I use a hidden value in the form, which is populated with the Text Box value, modified by the escape() function in Javascript.
This is my code currently, it works in every browser except for IE.
Any help would be much appreciated :)
<script language="jscript">
function changeTextBox()
{
hiddenSearch.value = escape(txtSearch.value);
formSearch.submit;
}
</script>
<form id="formSearch" name="Search" action="?page=search" method="post">
Search by <u>Part Code</u> or <u>Description</u>
<input id="txtSearch" type="text" size="35">
<input id="hiddenSearch" name="Search" type="hidden">
<input name="Submit" onclick='jscript:changeTextBox();' type="submit" value="Submit">
</form>
take a look at changes-- i think there were issues with your javascript.
<script language="jscript">
function changeTextBox()
{
document.getElementById("hiddenSearch").value = escape(document.getElementById("txtSearch").value);
this.submit();
}
</script>
<form id="formSearch" name="Search" action="?page=search" method="post" onsubmit='changeText()'>
Search by <u>Part Code</u> or <u>Description</u>
<input id="txtSearch" type="text" size="35">
<input id="hiddenSearch" name="Search" type="hidden">
<input name="Submit" type="submit" value="Submit">
</form>