PHP multiple forms - php

So the issue I'm running into is I have a project with nested forms to select options and for some reason I cant get it to get beyond the first form. If you run this and select the first button it correctly displays the second button, but after that it just returns to the beginning.
How do I do this correctly? I've tried various methods such as isset, using functions, wiping the $_POST variable, etc and to no avail. Would Google or Stackoverflow this but I'm not quite sure what this problem is called.
This is all being done within a single php file because I don't want to have to deal with leaving the page, and this started out as a simple assignment that I've greatly expanded to fit my needs. Also I know nothing of Javascript and have no interest in using it.
<html>
<body>
<?php
echo <<< HERE
<form method = "post">
<input type = "submit" name = 'button' value = 'Do thing 1'>
<br>
</form>
HERE;
$button = $_POST['button'];
if ($button == 'Do thing 1'){
echo <<< HERE
<br>
<form method = "post">
<input type = "submit" name = 'button2' value = 'Do another thing'>
</form>
HERE;
$button2 = $_POST['button2'];
if ($button2 == 'Do another thing'){
echo 'doing another thing';
}
}
?>
</body>
</html>

The way I would solve this kind of issue is by sending all the fields and naming the buttons:
<form>
<input name="input_1">
<input name="input_2">
<button type="submit" name="button" value="button">Button</button>
<input name="input_n">
<button type="submit" name="button" value="button2">Button2</button>
</form>
Then once submitted:
if($_POST['button'] == 'button') {
/* sanitize input 1*/
/* sanitize input 2*/
/* do something */
}
if($_POST['button'] == 'button2') {
/* sanitize what you need */
/* sanitize input n*/
/* do something */
}

Related

trouble with extracting form data from another php page

I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}

Parameter appears in URL only after second button click

I have strange problem with my search form. After I enter keyword and do the search request I get empty parameter value.
For example I type in the search field the word "something"
I see an empty value:
search.php?keyword=
After this I enter the keyword "else" and I recieve:
search.php?keyword=something instead of search.php?keyword=else
They somehow appear with "one step back"
I was trying to debug with print_r and var_dump but I only can print some values that does not explain my problem.
Am I missing something very trivial?
Here is what I have:
My class function:
public function show_search_result() {
$this->search_keywords = strip_tags($_GET['keyword']);
$this->_db->query("SELECT * from posts WHERE post_title LIKE '%$this->search_keywords%' OR post_content LIKE '%$this->search_keywords%' LIMIT 100");
$this->rows_results_found = $this->_db->resultset();
}
And my form:
<form action="search.php?keyword=<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>" method="post">
<input type="hidden" name="search_requested">
<input type="text" name="search_keywords" value="<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>"><input type="submit" value="Search">
</form>
<form action="" method=get>
<input type=text id=se>
<?php
if($_GET != null){
$sekw = $_GET ['se'];
$sql = //the query like='$sekw' limit=100;
?>
<input type=submit>
</form>
A simple code.
Your problem is when you send the form, it does save the keywords until the second send.
change your method from post to get. also i would advice you to use a framework for easy and fast coding. some include symfony2, laravel

Send value of submit button when form gets posted

I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done?
I boiled my code down to the following:
The sending page:
<html>
<form action="buy.php" method="post">
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<input id='submit' type='submit' name='Tea' value='Tea'>
<input id='submit' type='submit' name='Coffee' value='Coffee'>
</form>
</html>
The receiving page: buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some SQL database magic happens
?>
Everything except sending the submit button value works flawlessly.
The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Use this instead:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
The initial post mentioned buttons. You can also replace the input tags with buttons.
<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>
The name and value attributes are required to submit the value when the form is submitted (the id attribute is not necessary in this case). The attribute type=submit specifies that clicking on this button causes the form to be submitted.
When the server is handling the submitted form, $_POST['product'] will contain the value "Tea" or "Coffee" depending on which button was clicked.
If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).
<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>
To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.
At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().
Buy anyway , user4035 just beat me to it , his code will "fix" this for you.
Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
and in your php script
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] ) if you want to check if data was acctually posted.
You can maintain your html as it is but use this php code
<?php
$name = $_POST['name'];
$purchase1 = $_POST['Tea'];
$purchase2 =$_POST['Coffee'];
?>
You could use something like this to give your button a value:
<?php
if (isset($_POST['submit'])) {
$aSubmitVal = array_keys($_POST['submit'])[0];
echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
<input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>
This will give you the string "SomeValue" as a result
https://i.imgur.com/28gr7Uy.gif

How to catch & append a textbox value to a URL in this PHP code?

I managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?
Here's my code:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
?>
<script type="text/javascript">
var val1 = document.getElementById('val').value;
</script>
<?php
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
<form method="post" action="">
Enter value: <input type="text" name="value" id="val" /><br/>
<input type="submit" value="Submit" name="submit" />
</form>
</html>
Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)
As far as I understand your question, what you need to do is:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
$val1 = $_POST['val']; // TO store form passed value in "PHP" variable.
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
} // This is to end the "if" statement
?>
and then change the form input field to:
Enter value: <input type="text" name="val" id="val" /><br/>
I am not sure if POST accepts id values too!
Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.
Then change your PHP code to this:
if(isset($_POST['value'])) {
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}

php how to grab the selected value from a drop down list?

<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
i want to grab the selected value and place in in a var
any idea?
thanks
Depends on your form tag.
<form method="post">
Will pass the value to $_POST['gamelist']
While
<form method="get">
Will pass the value to $_GET['gamelist']
Ofcourse, only after hitting the submit button. As morgar stated, this is pretty basic form procession. I doubt you've used google or followed a tutorial, this is almost one of the first things one learn when working with forms and PHP. We arent here to give you full solutions, take this as an example and create the full page yourself:
if($_SERVER['REQUEST_METHOD'] == "Y") {
$choice = $_Y['gamelist'];
// other stuff you want to do with the gamelist value
} else {
echo '<form method="Y" action="file.php">';
// the rest of your form
echo '</form>';
}
Replace Y with either GET or POST.
$choice = $_REQUEST['gamelist']; //works with get or post
$choice = $_POST['gamelist']
if it is a POST, or $_GET if not.

Categories