On a series of web forms users needs to be able to move forwards and backwards. However I want to process the form irrespective of which direction they are going.
The buttonshave name of "submit" and values of "back" and next". How do I interpret the value?
This doesn't work :-)
if(isset($_POST[$submit['back']])) { header('location: ../pages/create_page1.php'); }
if(isset($_POST[$submit['next']])) { header('location: ../pages/create_page3.php'); }
The the name of the field is called submit, so you need to check if the field has been submitted and what its value is:
if ( isset($_POST['submit]') && $_POST[submit] == 'back' )
However you aren't using submit buttons, you are using server side image maps, so the value might not have been submitted (depending on the browser).
Instead you need to give the fields different names, and then check to see if co-ordinates were sent for those fields.
<input type="image" name="submit_back" src="../images/arrows/back_50.png" width="121" height="50" border="0" alt="Back" />
if ( isset($_POST['submit_back_x']) )
Try to use jquery:
$('#some_button').click(funnction(){
var $Data = $('#some-data-input').val();
if($Data.length > 0){ // or some else
window.location.href = "../pages/create_page3.php";
} else {
// other action
}
});
Try this javascript function...
<script language=javascript>
function redirect()
{
if(document.value=="Next")
{
document.form.action="next.php";
}
else if(document.value=="Back")
{
document.form.action="back.php";
}
return true;
}
</script>
<form method="post" name=form onsubmit='return redirect();'>
<p>Click...</p>
<input type="submit" name="back" value="Back"/>
<input type="submit" name="next" value="Next"/>
</form>
You can't get the button value with $submit, it should be $_POST['submit'].
You can try this script.
<?php
if(isset($_POST['next'])) {
$submit = 'Next button was clicked.';
// header(...);
} elseif(isset($_POST['back'])) {
$submit = 'Back button was clicked.';
// header(...);
} else {
$submit = '';
}
?>
<form action="" method="post">
<p>Click...</p>
<input type="submit" name="back" value="Back"/>
<input type="submit" name="next" value="Next"/>
</form>
<br/><?=$submit;?>
Or with same button name and arrows like on your comments:
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'next') {
$submit = 'Next button was clicked.';
// header(...);
} elseif(isset($_POST['submit']) && $_POST['submit'] == 'back') {
$submit = 'Back button was clicked.';
// header(...);
} else {
$submit = '';
}
?>
<form action="" method="post">
<p>Click...</p>
<input type="image" name="submit" src="http://cdn4.iconfinder.com/data/icons/brightmix/128/monotone_arrow_left_small.png" value="back"/>
<input type="image" name="submit" src="http://cdn4.iconfinder.com/data/icons/brightmix/128/monotone_arrow_right.png" value="next"/>
</form>
<br/><?=$submit;?>
Related
I'm trying to display one of two buttons depending on "magnum"(printername) being in present in the array "booleans".
My problem is that when the form gets posted, the data retrieved on page load is correct, but the buttons displayed are incorrect. if clicked on a button, the form posts and refreshes the page, "magnum" gets pushed into $_SESSION['booleans'] but the button still displays "btn btn-default", so it requires another page refresh for the button to load correctly('btn btn-succes').
Is my problem due to $_SESSION or am i missing something?
echo'
<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
if(isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']){
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans'][$pos]);
dump('unset');
}
}
elseif(!in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
if(isset($_POST['submitMagnum'])&& $_POST['submitMagnum']){
array_push($_SESSION['booleans'],'magnumBool');
dump('set');
}
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '
</div>
</form>';
$_SESSION['printers'] is an array containing "magnum" -
$_SESSION['booleans'] is the array which isn't working as i would like it to -
$enabled_printer = "btn btn-success" <br>
$disabled_printer = "btn btn-default" <br>
$lost_connection_printer = "btn btn-danger disabled"
The problem is that you are mixing elaboration and printing. Try to split you code, so it will work and it will be more readable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']) {
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans']['magnumBool']);
} elseif (isset($_POST['submitMagnum'])&& $_POST['submitMagnum']) {
$_SESSION['booleans']['magnumBool'] = true;
}
}
echo'<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(isset($_SESSION['booleans']['magnumBool'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
} else {
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '</div>
</form>';
P.s. note the use of "magnumBool" as a key isset instead of as a value: in this way (when possible) you will avoid duplicate entry and will make you code lighter if you have large arrays ;)
P.p.s. try always to keep you login separate from you template, this will make your code more readable and easier to maintain
Im trying to validate a single textbox on the same page using PHP but am unable to get the errors to show. When i click submit and there is nothing in the textbox it works by not letting it go to the next page but it does not show the errors. Here is my code.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
$errors="Please enter a IGN";
}
if (isset($errors)){
echo $errors;
}
}
?>
<form method="post" action="Queston1.php">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
I have tried it with and without the if (isset($errors)) to see if that was the problem but both times i get the same result.
Can anyone see or know how to fix this?
You should use jQuery or Javascript to validate before submitting the form.
But instead of
if($_SERVER['REQUEST_METHOD'] == 'POST'){
Try
$errors = '';
if(isset($_POST['IGN'])){
if($_POST['IGN'] == ''){
$errors .= 'Please Enter a IGN';
}
}
if($errors!=''){
echo $errors;
}
Try something like this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
echo "Please enter a IGN";
}
else {
header('Location: question.php');
}
}
?>
<form method="post" action="">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
Hi I have a working link to delete a row from my database.
<strong>Delete this Case</strong></td>
<?php
if($_POST['action']=="delete")
{
$id = $_POST['id'];
mysql_query("DELETE FROM rmstable2 WHERE id= '$id'");
echo("> Case # $id has been deleted as requested. To see your changes please click <a href='/martinupdate.php?id=$id'>here</a></b><br>");
}
?>
what I am looking to do is instead of having a link I want to have a button that when pressed it brings up a confirmation and then deletes the row if true.
I do not want accidental deletions.
<form>
<input type="button" value="Delete this Case" onclick="return confirm('Are you sure you want to delete?')";
<a href="?action=delete&id=<? echo $id ?>">
</form>
Try this at the top of your file:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'DELETE' || ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['_METHOD'] == 'DELETE')) {
$id = (int) $_POST['id'];
$result = mysql_query('DELETE FROM rmstable2 WHERE id='.$id);
if ($result !== false) {
// there's no way to return a 200 response and show a different resource, so redirect instead. 303 means "see other page" and does not indicate that the resource has moved.
header('Location: http://fully-qualified-url/martinupdate.php?id='.$id, true, 303);
exit;
}
}
With this as the form:
<form method="POST" onsubmit="return confirm('Are you sure you want to delete this case?');">
<input type="hidden" name="_METHOD" value="DELETE">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type="submit">Delete Case</button>
</form>
you have to put your confirmation in the onSubmit event of the form
so if the user cancel the confirmation, the form won't be sent
<form onSubmit="return confirm('Are you sure you want to delete?')">
<button type="submit" ...>
</form>
HTML:
<form id="delete-<?php echo $id; ?>" action="?action=delete" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="Delete this Case" />
</form>
JS im assuming jquery for ease:
$("#delete-<?php echo $id; ?>").submit(function() {
return confirm("Are you sure you want to delete?");
});
What this does is prevent the default submit action if the js confirm returns false (doesn't submit) otherwise lets the regular post go through.
Note: you really shouldn't use html attributes to declare event handlers, this code separates the logic.
EDIT: #Nicholas comment
This is a non-jquery solution. I didn't test it, and i don't believe that preventDefault works in IE <= 8 so I probably wouldn't use it in production BUT it could be done w/o too much code jquery just makes it cross browser and easier.
function loaded()
{
document.getElementById("delete-<?php echo $id; ?>").addEventListener(
"submit",
function(event)
{
if(confirm("Are you sure you want to delete?"))
{
event.preventDefault();
}
return false;
},
false
);
}
window.addEventListener("load", loaded, false);
I am having two buttons in a form ques and ans. When i click the ques button the same form with some text-fields will get open and in the form I've two buttons add and clear. By clicking add the entered field will be entered to the database. Like wise the same thing for clicking the answer button. I'm having one onsubmit=return valid() for both the buttons. I've to write separate script for both buttons, checking the conditions. But I'd written for only answer button. I dont know how to write for both buttons in one function valid(). Please tell me how to write for both buttons inside a function.
function valid()
{
var quest=document.getElementById('ques1').value;
var cno=document.getElementById('cno').value;
var pno=document.getElementById('pno').value;
quest1=trim(quest1);
pno=trim(pno);
cno=trim(cno);
if(quest=="")
{
alert("Please enter Question");
document.getElementById('ques1').focus();
return false;
}
else if( pno=="")
{
alert("Please enter valid Page No");
document.getElementById('pno').focus();
return false;
}
else if (cno=="")
{
alert("Please Select Chapter No");
document.getElementById('cno').focus();
return false;
}
}
You can do something like this
function validate(button_value) {
if(button_value=='test1') {
if(document.getElementById('rest1').value=="") {
alert('Blank for input 2');
return false;
}
} else if(button_value=='test') {
if(document.getElementById('rest').value=="") {
alert("Blank for input 1");
return false;
}
}
}
The HMTL I tested in:
<form method="POST" action="" onsubmit="return validate(this.submited);">
<input type="text" name="rest" id="rest" value=""/>
<input type="text" name="rest1" id="rest1" value=""/>
<input onclick="this.form.submited=this.value;" type="submit" id="test" value="test"/>
<input onclick="this.form.submited=this.value;" type="submit" id="test1" value="test1"/>
</form>
Hope this helps
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validate(this.submited);">
Question : <input type="text" name="ques">
Answer : <input type="text" name="ans">
<input type="submit" name="subques" value="Ask Question"/> <input type="submit" name="subans" value="Reply Answer"/>
</form>
<hr>
<?php
if(isset($_POST['subques']))
{
if(isset($_POST['ques']))
{
if($_POST['ques'] != '' && $_POST['ques'] != NULL)
{
echo "Your question is: " . $_POST['ques'];
}
}
}
if(isset($_POST['subans']))
{
if(isset($_POST['ans']))
{
if($_POST['ans'] != '' && $_POST['ans'] != NULL)
{
echo "Your answer is: " . $_POST['ans'];
}
}
}
?>
I am trying to do a submit form with photos. After the user loads the photos he presses the submit button, I want the form to pause for 10 seconds, animate a progress bar for those 10 seconds and then submit the form, can you guys say what I did wrong, it doesn't seem to submit the form after 10 seconds.
Here is the code:
HTML:
<form action="uploadpic.php" method="post" id="upload_form">
<input type="text" name="title" id="title">
<p id="title_p">Title</p>
<hr />
<input type="text" name="theme" id="picture_theme" size="40"/>
<p id="theme">Picture Theme<img src="../simages/info.gif" id="info" width="12" height="12" style="margin-left:10px;"></p>
<hr />
<div class="custom-upload">
<input type="file" name="picture" id="true_pic" />
<div class="fake-file">
<input disabled="disabled" >
</div>
</div>
<p id="upload_pic">Upload picture</p>
<input type="submit" name="submit" id="submit" value="Upload" />
</form>
JAVASCRIPT:
form = document.getElementById("upload_form");
size=1;
form.onsubmit = function()
{
if (size < 10)
{
setTimeout(delayedSubmit,1000);
}
return false;
}
function delayedSubmit() {
size++;
if (size<5)
{
setTimeout(delayedSubmit,1000);
alert("Counting "+size);
}
else
{
alert("Form submitted");
form.submit();
}
}
PHP :
<?php
if ($_POST['submit'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}
?>
I can tell that the form won't submit anything by the fact that the php variables won't show anything, and then page doesn't load.
When a form has a button with the name and/or id "submit", it won't work anymore (my old post was wrong).
So what you need to do is to change the name/id of the button:
<input type="submit" name="submit-button" id="submit-button" value="Upload" />
Remember: You need to change your PHP, too:
if ($_POST['submit'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}
to
if ($_POST['submit-button'])
{
$title = $_POST['title'];
$theme = $_POST['picture_theme'];
echo $title," ",$theme;
}
I'd simplify the javascript:
form = document.getElementById("upload_form");
size=0;
form.onsubmit = delayedSubmit;
function delayedSubmit () {
if (++size < 5) {
alert("Counting "+size);
setTimeout(delayedSubmit,1000);
return false;
}
alert("Form submitted");
form.submit();
}
And - of course - remove (or change) id and name from the submit button, e.g:
<input type="submit" value="Upload" />
e.g.: http://jsbin.com/equyit/1/edit