I have this code and it works.
echo "<input type='submit' name='liga' value='Liga'>";
if (isset($_POST['liga'])) {
unset($_POST['liga']);
liga();
?>
But I need change the name LIGA to the php variable $on. I tried this but don't work.How can I insert the variable in this code?
$on=1;
echo "<input type='submit' name='$on' value='Liga'>";
if (isset($_POST['$on'])) {
unset($_POST['$on']);
liga();
}
?>
The problem here is that variables inside single quotes don't get interpolated whereas variables inside double quotes do.
echo "<input type='submit' name='$on' value='Liga'>";
becomes
<input type='submit' name='1' value='Liga'>
But $_POST['$on'] stays the same. To solve this, use $_POST[$on] (or the equivalent $_POST["$on"]).
Related
<?php
if(isset($_POST['btnLogin'])){
$myVariable = $_POST['fieldParameter'];
if(condition){
//do something
}else{
echo "
<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>
";
}
}
?>
Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.
Also, i enclosed all the html tags using one echo statement with double quotes.
I found related questions here in SO but can't find similar to embedding php within a long echo of html tags
I tried to put value='<?php echo $studentNo; ?>' with no success.
I want to access it in a submit.php file like this,
submit.php
<?php
$aVariable = $_POST['myVariable'];
echo $aVariable;
?>
How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?
If you are already echoing a string you shouldn't put <?php echo "" ?> inside it again. You should concatenate your string instead. But in your case you don't even need to do that, because you're using double quotes for echoing which means you can simply just write your variable in it.
echo "<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='$myVariable;'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>";
If you were using single quotes for your echo, it would look like this:
echo '<form method="POST" action="submit.php"><br/>
<input type="hidden" name="myVariable" value="' . $myVariable . '"/><br/>
<input type="submit" name="btnSubmit" id="submit" value="Submit">
</form>';
You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the
You can try these variants (simplified):
// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after
// OR //
// code before
?>
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
<?php
// code after
Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).
echo "<td><input name='Send' type='button' value='Submit' onClick=location.href='save.php?myDocRegNo=$A&docCat=$C&dcrNo=$Dcr_No
&metTown=$Town_Code&dcrDate=$Dcr_Date&repCode=$Rep_Code&db=$Dbase' /></td>";
I have above link echoed in my php file and all my $ variables are set with values and the link works perfect.
But when I add two more variables &fullTown=$Town&fullRep=$FlRp to the same link and try to submit nothing happens and link does not work.Can anybody can identify the error ?Is there a variable limit for a HTML link?
echo "<td><input name='SendTest' type='button' value='SubmitTest' onClick=location.href='save.php?myDocRegNo=$A&docCat=$C&dcrNo=$Dcr_No
&metTown=$Town_Code&dcrDate=$Dcr_Date&repCode=$Rep_Code&db=$Dbase&fullTown=$Town&fullRep=$FlRp' /></td>";
please try this code its working fine
please try this code its workin fine
echo '<button onclick="window.location.href=\'save.php?myDocRegNo='.$A.'&docCat='.$C.'&dcrNo='.$Dcr_No.'&metTown='.$Town_Code.'&dcrDate='.$Dcr_Date.'&repCode='.$Rep_Code.'&db='.$Dbase.'&fullTown='.$Town.'&fullRep='.$FlRp.'\'">Continue</button>';
echo 'Continue';
please use this code it is working, i think there was only some blank space issue and i have change location.href change to window.location.href and it is working properly.
here is the code
echo "<td><input name='Send' type='button' value='Submit' onClick=window.location.href='save.php?myDocRegNo=$A&docCat=$C&dcrNo=$Dcr_No&metTown=$Town_Code&dcrDate=$Dcr_Date&repCode=$Rep_Code&db=$Dbase' /></td>";
Is it possible to use PHP variables as attributes in form inputs?
I have the following variable already declared:
$InStock //number of items in stock
I would like to use this variable per below, but the code isn't working (the max constraint is not being applied to the field):
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Is this legal, or am I attempting something syntactically impossible? Sorry if this is a stupid question - I'm new at this, and I embarrass myself frequently. : /
It's possible, your syntax is incorrect though. You're double dipping on the php brackets.
I usually prefer to do something like this instead of using print, it's easier to read, especially if your IDE is doing syntax highlighting:
?><td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td><?php
You could also do it within a print/echo statement, but you need to have the variable inline without the extra brackets:
echo "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
Note that without concatenation, this only works with double quotes.
Instead of your:
print "<td><input type='number' name='product1' id='product1' min='0' max='<?php echo $InStock ?>' value='0'></td>";
Use:
print "<td><input type='number' name='product1' id='product1' min='0' max='$InStock' value='0'></td>";
This is because PHP will insert the values of variables into a string if you put the variable name into the string and PHP will replace this with the value of your variable. For example print "Hi there, $name"could produce "Hi there, James" when executed - assuming $name is equal to "James".
Please check this Muddal: http://muddal.com/muddaltut.php?id=163
I have tried to search through the forums but I am a novice and am getting more confused.
I am trying to bring an input from a form and use it as a variable in a MySql query. A shortened version of the form is -
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input value=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
I am then trying to store the input into a variable using code -
$newVar = $_GET['leave'];
However I am getting an undefined index error.
Can anyone help with this? Sorry if its a very basic problem :)
The problem is with your HTML. You need to name the input.
echo '<input name="leave" class="text" autocomplete="off" type="text" value="' . $_SESSION['leave'] . '" />';
You declaring the "value attribute twice, you need to declare name:
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input name=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
echo '<form method="get" action="">';
echo "<tr><td>Leave:</td><td><input value='{$_SESSION['leave']}' class='text' autocomplete='off' type='text' name='leave'/></td></tr>";
echo "</form>";
If you use single quotes and doubles quotes alternating, you can make your code look nicer.
For your problem, you're missing your input name:
<input type=".." name="leave" ..>
Also, notice in your output of the field, you have the value set to the session value and an empty value near the end.
value=\"\"
I want to send data from one page to the other via a form in PHP. In the initial page I have included the following PHP script that generates an input form with hidden fields. The names of these hidden fields are generated by the PHP:
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='$software'></input>";
echo "<input type='hidden' name='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
In the second page, named process-results.php, I would like to get the names of these hidden fields via the $_GET method but of course using $_GET[$software] and $_GET[$version] wouldn't work...Can someone tell me if there is a solution to this issue or if there is a better alternative? Thanks in advance
Instead of
"<input type='hidden' name='$software'></input>";
you should use
"<input type='hidden' name='software' value='".$software."'></input>";
for each. This way, you can use $_GET['software'] to retrieve the value. Do this for each of your hidden inputs.
I think you may want something like:
<form ... >
<input type="hidden" name="software" value="<?php echo $software ?>" />
<input type="hidden" name="version" value="<?php echo $version ?>" />
</form>
and then
$_GET['software'];
$_GET['version'];
I'm not sure what you're trying to accomplish, but this looks odd to me. Isn't the below code more of what you're looking for?
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='software' value='$software'></input>";
echo "<input type='hidden' name='version' value='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
That way you will get a query string in form of ?software=yoursoftwarename&version=yourversion and it will be available via $_GET["software"] and $_GET["version"] on the next page.
You could iterate over each of the items in the $_GET array on process-results.php. The problem is that the keys for the value will be whatever $software and $version are set to on the first page. Try something like this:
foreach($_GET as $key=>$string) {
// Do stuff with them
}
Add
enctype="multipart/form-data"
To the tag... so it looks like
<form enctype="multipart/form-data" method......
If you really need to have the dollar-sign inside the name, escape it:
echo "<input type='hidden' name='\$software'>";
or put the string in single-quotes:
echo '<input type="hidden" name="$software">';
Otherwise PHP is looking for a variable named "$software", if you look inside the browser-source you will see that the name-attributes are empty(except you're having those variables defined somewhere).