I am done with my script using form submit. What I am doing is a series of questions per page with a Next Button. Now I would like to add a feature to go back to previous question.
How do I code this in one
<FORM name ="Submit1" method ="post" action = <? echo "question.php?num=". $n ; ?> >
<Input type = "Submit" VALUE = "Next" name ="Submit1" /></FORM>
I would like to add the button Previous to the same form without messing the alignment.
I tried making another and just posted the action to the previous page.
But the button is under the Next or above the questions.
<FORM name ="Submit1" method ="post" action="/question.php?num=<? echo $n; ?>" id="formID">
<Input type = "Submit" VALUE = "Next" name ="Submit1" />
<Input type = "Submit" VALUE = "Prev" onclick="document.formID.action = '/question.php?num=<? echo ($n-1); ?>'" name ="Submit2" />
</FORM>
Try with that Javascript
onclick="document.formID.action = '/question.php?num=<? echo ($n-1); ?>'"
This must change the Form action before your form is submitet, to the prev page $n-1 or perhaps it must be $n-2 .. You have to set it correctly
Related
I have a form of different types of input fields. It looks something like this:
<form action="function.php" method="POST" ...>
<select name="table" ...>
<option> ... </option>
<option> ... </option
</select>
<select name="column" ...>
<option> ... </option>
<option> ... </option
</select>
<input type="text" name="searchword">
<input type="button" name="operator" value="=" id="operator" onclick="change(this.id)">
<input type="submit" ...>
</form>
With my dynamic button, I can switch the operators (=, <, >) with my "change()" function which I need to create my queries. In my function.php file, I'm trying to get all the values of my input fields ...
$table = $_POST["table"];
$column = $_POST["column"];
$operator = $_POST["operator"];
... but unfortunately, it only works for the table and for the column input. I can't store the value of my operator button. I tried to find a solution for my problem but most people wrote that I have to change my button's input type to "submit" to pass the value. However, I do not want the action to be executed directly when this button is pressed, but only when the real "submit" button is pressed.
Edit:
Here is my "change()" function:
function change(operatorId) {
let element = document.getElementById(operatorId);
if (element.value == "=") {
element.value = ">";
} else if (element.value == ">") {
element.value = "<";
} else if (element.value == "<") {
element.value = "=";
}
}
And this is the error message I get when the function.php file opens: "Undefined array key "operator" in ..."
Edit: Solution
Thanks to Professor Abronsius' answer, I was able to resolve my problem. As he suggested, I inserted another hidden input field with the name "operator" and changed my button to "operator-selector". In this way, I just had to add some lines to my function to change the hidden field's value. This is how it looks like now:
<input type="hidden" name="operator" id="operator" value="">
<input type="button" name="select-operator" value="=" id="select-operator" onclick="change(this.id)">
function change(selectorOperatorId) {
let selector = document.getElementById(selectorOperatorId);
if (selector.value == "=") {
selector.value = ">";
document.getElementById("operator").value = selector.value;
} else if (selector.value == ">") {
selector.value = "<";
document.getElementById("operator").value = selector.value;
} else if (selector.value == "<") {
selector.value = "=";
document.getElementById("operator").value = selector.value;
}
}
What you could possibly do would be to keep the button as a regular button but change it's name and then add a hidden input named operator - the value can be assigned by the change function and will appear in the POST array.
Incidentally I had already written the alternative change function below before I saw the edited question. It does have the advantage of being easily extensible if other operators were required/possible.
If the button were changed to a submit the change function would need to have the event passed in as an argument and then call event.preventDefault(); to stop the form from actually being submitted.
const change=function(e){
e.preventDefault();//if the button was a `submit` button this would stop the form being submitted.
const operators=['=','>','<'];
const input=this.parentNode.operator;
let i=Number( this.dataset.i );
let j=i < operators.length-1 ? i+1 : 0;
this.dataset.i=j;
input.value=this.value=operators[j]
}
document.querySelector('input[type="button"][name="op-selector"]').addEventListener('click',change);
<form method='POST'>
<select name='table'>
<option>coffee
<option>dining
</select>
<select name='column'>
<option>doric
<option>corinthian
</select>
<input type='text' name='searchword' />
<input type='hidden' name='operator' />
<!--
the button appears the same but now sets the value of the real `operator`
field.
-->
<input type='button' name='op-selector' data-i=0 value='=' />
<input type='submit' />
</form>
Change input type from button to submit and then you can access button value with $_POST["operator"];
<input type="submit" name="operator" value="=" id="operator" onclick="change(this.id)">
If you're using a style framework and don't want your button type to get affected add another hidden field with text type and change its value programmatically.
value
Defines the value associated with the button’s name when it’s submitted with the form data. This value is passed to the server in params when the form is submitted using this button.
As documented here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
For a project, I need to generate a dynamic html form that will send POST info to a PHP page via the ACTION field.
The form has not to be static, it has to be dynamic so the user has to be able to generate(ADD) a not fixed(dynamic) number of input tags, then when all the inputs are generated and filled the user may click on submit button and send all the info to a php document via post.
I'm completely lost
I've been playing with this piece of code that generates the inputs but I'm not able to send the data via post to the php file
<script>
var choices=[];
choices[0]="one";
choices[1]="two";
function addInput(divName){
var newDiv=document.createElement('div');
newDiv.innerHTML="<input type='text'>";
newDiv.innerHTML=newDiv.innerHTML+"</input>";
document.getElementById(divName).appendChild(newDiv);
}
</script>
<form class="new" method="post" action="action.php">
<div id="dynamicInput">
</div>
<input type="button" value="Add" onclick="addInput('dynamicInput');" />
<input type="button" value="Save" />
</form>
To submit the form, one possible scenario is to change the type of Save button to submit:
<input type="submit" value="Save" />
Also add name property to your auto generated inputs. Otherwise you won't access the submitted $_POST data:
<input name="enter_name[]" type='text'>
As you see, I'm concatenating [] to the input field. That will convert all submitted data into an array. Otherwise, that last one auto generated input will overwrite the previous entered data.
just change your input type to submit instead of save
<input type="submit" value="Save" />
and you don't need to create new div each time to create an input you can either append it to the div you already have
document.getElementById(divName).innerHTML+= "<input type='text' name= 'added_input[]'/>";
or
var newInput=document.createElement('input');
// if you want each input in separate lines
newInput.style.display = "block";
newInput.setAttribute("name","added_input[]");
document.getElementById(divName).appendChild(newInput);
and in your php file you can get post values like this :
for ($i = 0; $i < $_POST['added_input']; $i++){
echo $_POST['added_input'][$i];
}
for more option to get post values see This Question
here i am getting a value from previous page with form here i assign the value to php variable $foodid i want to echo its value after the continue button is clicked
//its value is passed from the previous page form with action to this page
$foodid = $_REQUEST['foodid'];
//as soon as continue button is clicked i want to display $foodid
<form method="post" action="">
<input type="submit" name="continue" value="continue">
</form>
if(isset($_POST['continue'])){
echo $foodid;//here the foodid variable must be declared
}
PHP is a server side languaue
JAVASCRIPT - is a client side language
After redirecting to new page , you have the value with your self, but displaying it on click is possible with javascript only (will display the number without refreshing the page)
in PHP - its not impossible, but it does not make sense to redirect to same page with some additional parameters's to display
eg; on click continue , submit a form with no action and there form should have that input field with value which you want to display (can be in hidden type), it will get submitted to same page and you will get your value using
$_REQUEST['field_name'];
But its not recommended , use JS for this, people purposely use JS for such kind of things
You should pass the $foodid value through form to the php page. This can be done by declaring a hidden variable and assigning foodid value to it.
Try this
<?php
$foodid = $_REQUEST['foodid'];
?>
<form method="post" action="">
<input type="hidden" value="<?php echo $foodid ?>"
<input type="submit" name="continue" value="continue">
</form>
<?php
if(isset($_POST['continue'])){
echo $foodid = $_POST['foodid'];
}
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
I am quite new at programming (especially in PHP).
Anyway, I have been attempting for a bit now to create a log-in page that utilizes both PHP and HTML.
The main idea is if it's the first time the user has come to the log-in page, then they have to click a "submit" button first; which will then reload the page but now will have a where the user will input information to call up another PHP script.
I have attempted the following codes to achieve this:
<form method = "post"
action = "Sign-In2.html">
<p>
<br />
<input type = "submit"
name = "returnUser"
value = "Click To Log In">
</P>
</form>
<?php
if (isset($_POST['returnUser'])) {
?>
<form method = "post"
action = "Login2.php">
<p>
<br />
In Honor Of:
<input type = "text"
name = "userName"
value = "">
<br />
EDGE Code:
<input type = "password"
name = "edgeCode"
value = "">
<br /><br />
<input type = "checkbox"
name = "rememberUser"
value = "remembered">Remember Me
<br />
<input type = "submit"
value = "Log Me In">
</p>
</form>
<?php
} else {
} //end if
?>
From everything I have read online (from this site and others), I am under the impression that my code includes two forms and a PHP "if" statement.
The FIRST form just provides a "submit" button that is named "returnUser" and reloads the page (which is named Sign-In2.html).
Then PHP jumps in and runs a condition check. If the "submit" button from the FIRST form has in fact been clicked, then the SECOND form will be run. If it has NOT been clicked, then nothing happens.
SO MY ISSUE is that when I run this this code from localhost WAMPserver, BOTH forms show up! I am unable to understand or find out why not just the first form but both are showing up when I load the page! I just want the first submit button to show up so I can click on it and refresh the page with the input fields from the second form :(
Any help would be highly appreciated!
Note: The second form and its phpscript both run no problem.
You want to display one form or the other, so use the if statement to choose which one you want to display. What your code is doing at the moment is displaying the first form - then if you have a $_POST value sent, displaying the second one.
Changing your code to the below will display one form or the other, depending on if the $_POST is set or not.
<?php
if (isset($_POST['returnUser']))
{
?>
<form method = "post"
action = "">
<p>
<br />
In Honor Of:
<input type = "text"
name = "userName"
value = "">
<br />
EDGE Code:
<input type = "password"
name = "edgeCode"
value = "">
<br /><br />
<input type = "checkbox"
name = "rememberUser"
value = "remembered">Remember Me
<br />
<input type = "submit"
value = "Log Me In">
</p>
</form>
<?php
}
else
{
?>
<form method = "post"
action = "">
<p>
<br />
<input type = "submit"
name = "returnUser"
value = "Click To Log In">
</P>
</form>
<?php
} //end if
?>
change the File name Sign-In2.html to Sign-In2.php and same in form action.
Is this by any chance a normal .html page?
By default, PHP doesn't run on those, so your <?php ?> tags are interpreted as normal HTML tags.
Change the file's extension to PHP to solve it.