choosing checkbox echoing submit button immediately - php

what is the code if I have checkbox and by choosing it(without any submit button) showing me a submit button
<html>
<head>
<title>test</title>
<form method="post">
<?php
//what will be the code here?
?>
<input type='checkbox' name='cb'>
</form>
</html>

Simply CAN'T DO without Javascript. With Javascript?
Easy, quick, sweet and approachable
<script>
function openinput() {
document.getElementById('submit').style.visibility = "visible";
}
</script>
<form>
<input type="checkbox" onclick="openinput()" />
<input type="submit" id="submit" value="go" style="visibility: hidden" />
</form>

Not a JS guy but something simple like this should work (untested code):
<script>
function showHide(obj){
if(obj.checked==true){
document.getElementById('submit_btn').style.display = 'inline';
}else{
document.getElementById('submit_btn').style.display = 'none';
}
}
</script>
<form method="post">
<input type='checkbox' name='cb' onclick="showHide(this)">
<input type="submit" id="submit_btn" value="submit" style="display:none" />
</form>
Ofcourse there are more elegant solutions using frameworks like JQuery

Something like this, but this will only work if you indeed submit the form but don't do anything with the post values other than this check. JavaScript would be the better option.
<?php
if(isset($_POST['cb'])) {
?>
<input type="submit" value="go power rangers" />
<?php
};
?>

raw code: (in jQuery v1.6)
$('input[type="checkbox"]').change(function() {
var $input = $(this);
if($input.prop('checked')){
alert('checked');
$('input[type="submit"]').show();
}
else
$('input[type="submit"]').hide();
});
DEMO

Related

isset() function not working properly for me

So I have PHP code like this
if (isset($_POST['btnChange'])) {
PHP CODE HERE
} else if (isset($_POST['btnUpdate'])) {
PHP CODE HERE
} else {
PHP CODE HERE FOR DELETE
}
My HTML code loks like this
<form action="decision.php" method="POST">
SOME HIDDEN INPUTS AND ON TABLE
<input type="submit" name="btnChange" value="Change">
<input type="submit" name="btnUpdate" value="Update">
<input type="submit" name="btnDelete" value="Delete">
</form>
So the problem is that when I click each button I straight go for DELETE section (else), my code is not checking for if-s.
I checked my code and it is correct only problem is probably in if(isset).
The displayed code look OK. Surely something you haven't shown us.
Just try your lines in a simple test file to be sure.
<?php
if (!empty($_POST)) {
if (isset($_POST['btnChange'])) {
echo 'btnChange';
} else if (isset($_POST['btnUpdate'])) {
echo 'btnUpdate';
} else {
echo 'else';
}
}
?>
<form method="POST">
<input type="submit" name="btnChange" value="Change">
<input type="submit" name="btnUpdate" value="Update">
<input type="submit" name="btnDelete" value="Delete">
</form>
Belive it or not,
my form looked like this
<form action="decision.php" action="POST">
Instead of
<form action="decision.php" method="POST">
Thank you all for taking time for this and sorry for wating you time, I was to tired.

Go to the url of the selection after submitting the form

I have the following code so depending on the selected option, the visitor is directed to a new page on change. But I want them to be directed to that page after they've clicked on the submit button, not immediately on change. How can I do that?
<script type="text/javascript">
function go()
{
window.location=document.getElementById("link").value
}
</script>
<form>
<select id="link" onchange="go()" > <option>DEFAULT</option>
<option value="page1.php">TITLE1</option>
<option value="page2.php">TITLE2</option>
<option value="page3.php">TITLE3</option>
</select>
<div class="submit"> <input name="submit" type="image" id="submit" src="images/getaquotebutton.png" height="93" width="259"/> </div><!--end of submit class-->
</form>
Many thanks in advance
This is simple:
Remove the onchange from here:
<select id="link" onchange="go()" >
Instead add:
<input onclick="go()" name="submit" type="image" id="submit" src="images/getaquotebutton.png" height="93" width="259"/>
Edit:
Just noticed the type is set to image, well if this doesn't work as intended, try to change the type to button and add image through css as:
<input onclick="go()" name="submit" type="button" id="submit" style="background-image: url('images/getaquotebutton.png'); height:93px; width:259px;" />
With jQuery you can do something like this:
$('#submit').click(function(
window.location=document.getElementById("link").value;
));
Of course you should remove the onchange.
$(function() {
$('form').on('submit', function() {
if($('#link').val() == page1.php) {
do redirect..
}
});
});
Do like this for each!

Hide a form field with a specific id after submit

I'm new here and a super noob in programming. I'm having trouble with my project. My problem is that I'd like hide the form after submit and retain the data input in it.
Here's my code:
<?php
$displayform = true;
if (isset($_POST['trck']))
{
$track = addslashes(strip_tags($_POST['tracknumber']));
$ord = $_POST['id'];
$displayform = false;
if (!$track)
echo "Please enter your tracking number!";
else
{
mysql_query("update `orderdetails` set `trackno`='$track' where `id`='$ord'");
}
if ($row2['id']==$ord)
echo $_POST['tracknumber'];
}
if ($displayform)
{
?>
<form method="post" action="">
<input type="text" name="tracknumber" id="tracknumber" size="30" maxlength="30" placeholder="Enter your track number here." />
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>">
<input type="submit" name="trck" id="trck" value="Save" onclick="return confirm(\'Are you sure you want to save this tracking number?\');" />
</form>
</td>
</tr>
<?php
}
}
?>
This code was inside a while loop and my problem with this is that after I submit all the form is hidden. All I want is to hide the form with the specific ID on a query.
Simplest way is to use jQuery hide
Include the jquery as
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
$("#buttonid").click(function(){
$("#formid").hide();
});
You are looking for something like this in your <form> tag:
<form method="post" action="" id="awesome_form" onSubmit="document.getElementById('awesome_form').style.display = 'none';">
use the jQuery and use :
$("#submitButtonId").click(function(){
$('#formId').hide();
});
or else in pure javascript use
<input type="submit" value="submit" onClick="hideIt()"/>
<script type="text/javascript" >
function hideIt() {
document.getElementById('formId').style.display = 'none';
}
</script>
its better not to use inline javascript

Form POST action wont work

Why wont this work?
<form action="" method="POST">
<input type="text" class="searchBar" name="domain" /><input type="button" value="SEARCH" class="searchButton" id="srch" />
</form>
<? print $_POST['domain']; ?>
I have tried putting it into a var too. I just got an error.
Thanks
Edit:
Suggestions have been made of changing button to submit, but my jQuery bug's up.
<script type="text/javascript">
$('#srch').click(function(){
$('#notActive').attr('id','Active');
});
$(document).ready(function(){
// Hide div 2 by default
$('#Active').hide();
$('#srch').click(function(){
$('#notActive').hide();
$('#Active').fadeIn();
});
});
</script>
I have managed to change the Button to Submit by using the action as javascript: void(0); for my form.
The form cant submit if contains no type="submit" input.
<input type="button" /> cant submit form. You should add <input type="submit" /> to form.
You can't access the posted variable before a form is submitted.
if (isset($_POST['domain'])) {
// a form is submitted that contains the 'domain' parameter
print $_POST['domain'];
}
Try setting an action:
<form action="index.php" method="POST">.
Also, change your second line to this:
<? if (isset($_POST['domain'])) echo ($_POST['domain']); ?>

Retrieving already assigned text box value dynamically in javascript

I have a form in PHP which looks like this:
<?php
$txtVal1 = "value1";
?>
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
</script>
<form method="post" action="">
<input type="text" value="<?php echo $txtVal1;?>" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
My question is whenever I click "Post" button I get an alert message as "value1". This is fine. But now if I change the text box value to something else from the UI I still get the old value. Is there any solution to get the changed value?
Try this. this works. check if your php value is putting value correctly or not
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
</script>
<form method="post" action="">
<input type="text" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
<pre>
<?php <br/>
$txtVal1 = "value1";<>
?>
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
function test(val){
document.getElementById("text1").value = val;
}
</script>
<form method="post" action="">
<input type="text1" value="<?php echo $txtVal1;?>" onChange="test(this.value)" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
Try this jQuery fiddle
It may be overkill but it works. StackOverflow doesn't like links to jsfiddle?
$("#submit").click(function(){
alert($("#text1").val());
});
Your page when ever load the first php value assign and after that run all value that is the problem for that. You change to assign value for the above one $txtVal1 = "value1";

Categories