Ask if you are sure you want to submit? JavaScript? - php

I currently have this.
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Redeem' value='Redeem'></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Un-Redeem' value='Un-Redeem'></form></td>";
//other stuff down here - not important
and I want to change it so that when you press:
a) the 'Redeem' SUBMIT button it alerts you saying, "Are you sure you want to Redeem?"
b) the 'Un-Redeem' SUBMIT button it alerts you saying, "Are you sure you want to Un-Redeem?"
I have tried a few of them including the ONCLICK on the submit but none worked.. and I believe that is because I have it in an ECHO and I had to remove the (") quotation marks which stopped the function from happening.
Anyone know another way I can do it?

You can use the Javascript confirm function.
if(confirm("Are you sure you want to Redeem?")) {
// do something
} else {
// do something
}
You can also do this on form submit by adding the following code to your form:
onsubmit="return confirm('Are you sure you want to Redeem?');"
The form will only submit if the user clicks "OK".

Here is the solution :
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Redeem' value='Redeem' onclick="return confirm('Are you sure you want to Redeem?')"></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Un-Redeem' value='Un-Redeem' onclick="return confirm('Are you sure you want to Un-Redeem?')" ></form></td>";
//other stuff down here - not important
Edit:
Added escaping character in here:
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Redeem' value='Redeem' onclick=\"return confirm('Are you sure you want to Redeem?')\"></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Un-Redeem' value='Un-Redeem' onclick=\"return confirm('Are you sure you want to Un-Redeem?')\" ></form></td>";
//other stuff down here - not important

Use the javascript confirm function. The form won't submit if the confirm returns false.
<form action='redeem.php' method='post' id='form' onSubmit="return confirm('Are you sure you want to redeem')">
If you want to do something else if the user clicks cancel then you'll need to create a custom function:
function my_confirm() {
if( confirm("Are you sure you want to redeem?") ) {
return true;
}
else {
// do something
return false;
}
}
And on the form tag:
onSubmit="return my_confirm()"

Related

How to disable submit button once it has been clicked php?

I have a send button at the end of the form but I do not know how to disable it in the onClick.
echo "<input type='submit' name='add' value=\"".$LANG['help'][14]."\" class='submit' >";
you can use
echo "<input type='submit' name='add' onclick="this.form.submit(); this.disabled = true;" value=\"".$LANG['help'][14]."\" class='submit' >";
You can try following code
echo "<input type='submit' name='add' onclick="this.disabled = true; this.form.submit();" value=\"".$LANG['help'][14]."\" class='submit' >";
please try this
$('input[type="submit"]').click(function() {
this.disabled = true;
};

How to use string from condition1 into condition2?

I have the following code:
<form action='' method='POST' id='form1'>
imdbcode : <input type='text' id='imdbcode' name='imdbcode' /><br/>
<input type='submit' name='submit' value='Get'/>
</form>
<?php
if(isset($_POST['submit'])){
.
.
.
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
//I WANT TO USE $Title from condition1
} ?>
I want to use $title in second condition.
It prints $title in first condition! But doesn't print after closing condition!
How can I do that?
You can't.
Before you can enter the if(isset($_POST['Send']))
you need submit this one:
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
but the moment you submit this, the page will be refreshed and $_POST['submit'] will be deleted, without this variable the $title will not exist.
to fix this or make the $title value alive until the next refresh of the page. you must include the $title value along with the form2.
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value="$title"/>
<input type='submit' name='Send' value='Send'/>
</form>";
and when the user send that form2 you can access the title by using
$_POST['title']
As two your forms are different - there's no connection between them unless you explicitly provide it.
Simple solution can be just echo your $title as a hidden field in a form:
if(isset($_POST['submit'])){
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value='" . $title . "'/>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_POST['title'];
// do other stuff
}
Another solution is to use sessions:
if(isset($_POST['submit'])){
$title = ... ;
$_SESSION['title'] = $title;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_SESSION['title'];
// do other stuff
}
In case of using sessions don't forget to use session_start and probably to unset $_SESSION['title'] in the end of second submit.
<form action='' method='POST' id='form2'>
<input type='hidden' name='text' value='<?php echo $title; ?>
<input type='submit' name='Send' value='Send'/>
</form>
Try to add a hidden input before the Send button.

two submit buttons in one form of type 'image'

I have a form in PHP with two submit buttons of type 'image'.
How do I know which button is clicked?
Code:
<?php
echo ("
<form action='WijzigSession.php' method='POST'>
<td><input class= 'nummers' type='number' name='aantal' value=".$_SESSION['producten'][$i]['aantal']." min='1' max='20'>
<input name='refresh' class='buttons' type='image' src='img/refresh.png'</a>
<input name='delete' class='buttons' type='image' src='img/delete.png'</a>
<input type='hidden' name='id' value='$i'>
</form></td>
<td>€ ".$_SESSION['producten'][$i]['prijs']."</td>
");
?>
Just try:
if(isset($_POST['refresh_x'])) { //you can potentially even check for '$_POST['refresh_y']'
// refresh is clicked
}
if(isset($_POST['delete_x'])) {
// delete is clicked
}

Issues with PHP INSERT form

So I'm trying to get this php code to display a textarea and submit button so a user can submit a bio that gets inserted into a db. I have a few issues with this code:
For some reason, the text area is filled with the actual form HTML instead of just a blank box, so it shows as:
<form action='page-bio.php' method='post'>
<textarea name='author_bio' value=<input type='hidden' name='hidden' value=
<input type='submit' name='update' value=update
</form>.
I'm using wordpress, so this is a php template, but I am wondering if this code will properly get the current user and insert the user's bio into the user's bio field in the database? I have a feeling it won't, but I can't tell because I still can't get the submit button to display.
<?php
$con = mysql_connect("localhost","XXXX","XXXX");
if (!$con){
die("can not connect: " . mysql_error());
}
mysql_select_db("i5412",$con);
// Get the current user's info
$current_user = wp_get_current_user();
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE wp_usermeta SET author_bio='".$_POST['author_bio']."'WHERE
user_id=$current_user and author_bio='".$_POST['hidden']."'";
mysql_query($UpdateQuery, $con);
}
$sql = "SELECT * FROM wp_usermeta";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
echo "<form action='page-bio.php' method='post'>";
echo "<textarea name='author_bio' cols='10' rows='10' value=" . $record['author_bio'];
echo "<input type='hidden' name='hidden' value=" . $record['author_bio'];
echo "<input type='submit' name='update' value=update";
echo "</form>";
}
mysql_close($con);
?>
You need to change the following lines:
echo "<textarea name='author_bio' cols='10' rows='10' value=" . $record['author_bio'];
echo "<input type='hidden' name='hidden' value=" . $record['author_bio'];
echo "<input type='submit' name='update' value=update";
To
echo "<textarea name='author_bio' cols='10' rows='10'>" . $record['author_bio'] . "</textarea>";
echo "<input type='hidden' name='hidden' value='" . $record['author_bio']. "'>";
echo "<input type='submit' name='update' value='update'>" ;
you forgot to close half of the input tags such as echo "<input type='submit' name='update' value=update"; where you missed the ending >

PHP variable as input value

I'm trying to make a voting system. The form below is located in an echo. So I want the $entree_id to be filled in as the value of the hidden field and stored in a database when someone clicks on the submit button. But whenever I try it, the row stays empty.
This is the database output:
$entree_id = $row['entree_id'];
And this is in the echo:
<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>
I have also tried something like this:
<form action='/' method='post'><input type='hidden' name='entree_id' value='<?php print $entree_id ?>'><input type='submit' class='button'></form><p>
Why don't I get the value of $entree_id?
you must distinguish between html and php. your variables should be betwen php tags.
try this
<form action='' method='post'><input type='hidden' name='entree_id' value='<?php echo $entree_id ; ?>' /><input type='submit' class='button' /></form><p>
EDIT:
inside the echo then it will be like this
echo "<form action='' method='post'>
<input type='hidden' name='entree_id' value='".$entree_id."' />
<input type='submit' class='button' />
</form>" ;
make sure that type is hidden .
change it to
type='text' //or what ever to see the value.
Both of those SHOULD work if there is already a value in $entree_id at the time PHP is running. If you do the first one you were talking about...
<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>
... then you will need it to be part of an echo statement (which it sounds like you might already be doing):
<?php
$entree_id = 5;
echo "<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>";
?>
(Don't forget the semicolon at the end!) This produced output for me like this:
<form action='/' method='post'><input type='hidden' name='entree_id' value='5'><input type='submit' class='button'></form><p>
But you could also do it the 2nd way you were saying, I think this might be more readable. You don't have to use an echo statement for the whole thing, just for the variable:
<?php
$entree_id = 5;
?>
<form action='/' method='post'><input type='hidden' name='entree_id' value='<?php echo $entree_id; ?>'><input type='submit' class='button'></form><p>
Notice I changed your "print" to an "echo" and I also added a semicolon at the end of the echo.
This worked for me too, producing this output:
<form action='/' method='post'><input type='hidden' name='entree_id' value='5'><input type='submit' class='button'></form><p>
Hope this helps! Let me know if you have any other questions.
Change your echo to use single quotes, instead of double quotes, like so:
echo '
<form action="/" method="post"><input type="hidden" name="entree_id" value="' . $entree_id . '"><input type="submit" class="button"></form>';
double quotes can be used in php for outputting variables automatically. Using Single quotes makes this less confusing and ensures that the . is concenating to it properly.

Categories