I have a button that I need to call a function on when it is clicked but for some reason, the isset function is not seeing the button as being clicked.
Below are the two lines in the php/html file:
<input type="submit" id='details' name='details' class="pull-right btn-sm btn-primary" value='Show Details'/>
<h4><?php echo $subject_details; ?></h4>
The below is in an external. I know it is included because if I add an else to the if statement, and set that variable to something different, it will change.
$subject_details = '';
if(isset($_POST['details'])){
$subject_details = 'button clicked';
setDetails();
}
Thanks to anyone looking at this!
The isset() function only works for input type submit. What you can do is embed the button in a form and let the page reload to itself to grab the variable stored in an external php. As an example:
//in your main.php
<?php
include 'external.php';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Send">
</form>
<h4><?php echo $subject_details; ?></h4>
//in your external.php
<?php
$subject_details = '';
if(isset($_POST['submit'])){
$subject_details = 'button clicked';
}
?>
Without seeing the full code example, I can't be sure, but it's possible you're not specifying the method="post" attribute on the <form> tag. A form defaults to method="get" therefore you would need to use isset($_GET['details']) instead.
A working example would be:
<form action="" method="post">
<input type="submit" name='details' value='Show Details'>
</form>
<?php
if (isset($_POST['details'])) {
echo $_POST['details'];
} else {
echo 'No post data submitted';
}
you could play around with this... might help
<!-- head section -->
<script>
window.onload=function(){
var oCol=document.querySelectorAll('input[type="button"]');
for( var b in oCol ) if( b && oCol[b] && oCol[b].nodeType==1 ) oCol[ b ].onclick=function(e){
alert( this.tagName + ' '+this.id+' was clicked...' );
}.bind( oCol[b]);
};
</script>
<input type='button' id='b1' value='Button 1' />
<input type='button' id='b2' value='Button 2' />
<input type='button' id='b3' value='Button 3' />
<input type='button' id='b4' value='Button 4' />
Related
I have this HTML button:
<form action="testscript.php" name ="more" method="get">
<input type="submit" value="More Details">
</form>
Which when clicked runs this PHP script:
<?php echo 'button pushed: ';
echo $buttonvalue;
?>
My question is, how to I assign a value to the button so that when its clicked it will forward its value ($buttonvalue) to the PHP script
For example when the button is clicked i want it to run the script and the result should be button pushed: blue
How do i assign "blue" to the button?
you can try this
HTML :
<form action="testscript.php" name ="more" method="get">
<input type="hidden" name="buttonvalue" value="Some Value">
<input type="submit" value="More Details">
</form>
PHP :
<?php
echo 'button pushed: ';
echo !empty($_GET['buttonvalue']) ? $_GET['buttonvalue'] : '';
?>
Use anchor tag with 'btn' class. And pass value to next page with id.
Page 1.
<a class="btn btn-primary" href="next-page.php?id=value">Click me</a>
Page 2.
$buttonvalue = $_GET['id'];
echo $buttonvalue;
<form action="testscript.php" name ="more" method="get">
<input type="hidden" name="getDataName" value="123">
<input type="submit" value="More Details">
</form>
PHP script:
<?php
$buttonvalue= isset ($_GET['getDataName']) ? $_GET['getDataName'] : '';
if(!empty($buttonvalue)){
echo 'button pushed: ';
echo $buttonvalue;
}else{
echo 'button not pushed: ';
}
?>
I'm trying to wrap a function like <input type = "submit"/> into my
<a href="generate.php?employeeNo=< ?=$row['employeeNo']?>">
Generate
< /a>
I tried doing this.
<form method = "POST">
<input type = "text" name = "payslipPeriod">
<a href="generate.php?employeeNo=<?=$row['employeeNo']?>">
<input type = "submit" value = "Generate Payslip">
</a>
</form>
$getPayslipPeriod = $_POST['payslipPeriod'];
But i never get the value from the form and echo it to the next page.
This is may help:
In page 1:
<?php
if (isset($_POST['payslipPeriod'])) {
?>
<script>
window.location.href="generate.php?employeeNo=<?php echo $_POST['payslipPeriod'] ?>";
</script>
<?php
}
?>
...
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<label for="payslipPeriod">...</label>
<input type="text" name="payslipPeriod">
<button type="submit" name="submit">Submit</button>
</form>
...
Now use this in page 2:
$_GET['employeeNo']
I would like to add an input element to the form dynamically using only PHP.
I know how to make this using php and JavaScript combination, thus do nto advice abotu JavaScript.
The example below does not work. Could you please advice and comment:
input.php
<br> <input type="text" name="mob[]" value="" size="3" >
form.php
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount'];
$AddNumCount=$AddNumCount+1;
echo $AddNumCount;
}
if( isset($_POST['register']) ){
print_r($_POST['register']);
}
if (!isset($AddNumCount)) {$AddNumCount=5;}
?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<br>
<?php for ($i=0; $i<$AddNumCount; $i++) { Include('input.php'); } ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value=" <?php $AddNumCount; ?> "> </p>
<br></form><input type="submit" name="register" id="regcont" value="register"> </p>
</form>
Maybe you know how to make single submit button for many forms?
I mean each input would be a separare form and all forms can be submittted with the button on the end?
You use two action attrs. Maybe you mean:
<form method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
For submitting many forms by one button - you need to use JavaScript and send them in cycle via AJAX.
I am sorry i change this post. This is working example for dynamical PHP.
Use EXform.php. Other files are generated or helping.
Maybe it is also possible to make this using Session variables and header for redirecting to regenerated webpage.
EXform.php
<?php if (isset( $_POST['AddNum'])) { Include("GENinput.php"); } ?>
<?php if (!isset( $_POST['AddNumCount'])) { $_POST['AddNumCount']=1; Include("GENinput.php"); } ?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<?php Include("INCinput.php"); ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value="<?php echo $AddNumCount; ?>"> </p>
<input type="submit" name="register" id="regcont" value="register"> </p>
<br></form>
</form>
GENinput.php // generates included file
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount']; //=
$fnameinp="INCinput.php";
$fileinp=fopen($fnameinp,"w");
$_POST['AddNumCount']=$AddNumCount=$AddNumCount+1;
//echo "AddNumCount=".$AddNumCount;
$strV=""; $stri="";
for ($i=0; $i<$AddNumCount; $i++) {
$strV.=" \n
<?php
if( isset(\$_POST['v']['tname']['colname'][".$i."]) )
{ \$v['tname']['colname'][".$i."]=\$_POST['v']['tname']['colname'][".$i."];}
else { \$v['tname']['colname'][".$i."]=".$i."; }
?>
";
$stri.=" <br> <input type=\"text\" name=\"v[tname][colname][".$i."]\" value=\"<?php echo \$v['tname']['colname'][".$i."]; ?>\" > \n\n";
}
fwrite($fileinp,$strV);
fwrite($fileinp,$stri);
fclose($fileinp);
}
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
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