Pass code using POST method - php

I made a form that receives source code that can be written in C/C++/Python/Java/Ruby
<?php
if(isset($_GET['id']))
{
$problem_code=$_GET['id'];
echo '<form name="submit-button" action="/codejudge/result-board.php?id='.$problem_code.'" method="post">';
}
?>
<textarea id="editor" class="editor" rows="25" cols="100" name="code">
</textarea>
<select id="lang" name="lang" class="lang">
<option value="11" selected>C</option>
<option value="41">C++</option>
<option value="4">Python 2</option>
<option value="116">Python 3</option>
<option value="55">Java</option>
<option value="17">Ruby</option>
</select>
<button type="submit" >Submit</button>
</form>
and here is the code for result-board.php
<?php
include "conn_db.php";
$code=stripcslashes($_POST['code']);
$lang= stripcslashes($_POST['lang']);
echo $code.$lang;
?>
What should I use in case of POST method so that correct data can be receive.

I think you can also try:
<?php
if(isset($_GET['id']))
{
$problem_code=$_GET['id'];
}else{
$problem_code=0;
}
echo '<form name="submit-button" action="/codejudge/result-board.php" method="post">';
echo '<input type="hidden" value="'.$problem_code.'" name="id"/>';
?>
<textarea id="editor" class="editor" rows="25" cols="100" name="code"></textarea>
<select id="lang" name="lang" class="lang">
<option value="11" selected>C</option>
<option value="41">C++</option>
<option value="4">Python 2</option>
<option value="116">Python 3</option>
<option value="55">Java</option>
<option value="17">Ruby</option>
</select>
<button type="submit" >Submit</button>
</form>

your form should be something like:
<form name="submit-button" action="/codejudge/result-board.php' method="post">
and then add your problem code like,
<input type="hidden" value="<?php echo $problem_code;?>" name="code"/>
and then access with your name like,
$_POST['code']

Related

Get inputs from a drop down to php

I am trying to make a dropdown list and take three parameters from the user which will be stored in a php file for later use. My current code is :
<body>
<header>
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First">
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Second</label>
<select name="Second">
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Third</label>
<select name="Third">
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
</form>
</header>
</body>
Here, the problem is, I don't want submit button for each parameter, instead there should be only one button for all the parameters. I have tried several things but none is working.
My php file
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
Where am I going wrong?
EDIT : Corrected the php code
Just wrap all your selects under one form instead of 3 different forms.
Make sure that the input submit is not inside the select tag.
Also, note that your php code is trying to echo parameters that don't exist in the html you shared. The name of your select will be the parameter name.
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
you can add all there in one form
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
<input type="submit" name="submit" value="submit">
</form>
in your code you have missed button "name".
parameter.php code should be this.
if(isset($_POST['submit'])){
echo $First = $_POST['First'];
echo $Second = $_POST['Second'];
echo $Third = $_POST['Third'];
}
<form action="parameter.php" method="post" id="frm">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
JQuery AJAX
<script>
$('#frm').on('submit', function(e){
e.preventDefault();
$.ajax({
url : $(this).attr('action'),
type: "POST",
dataType: "json",
data : $(this).serialize(),
success: function(data)
{
console.log(data)
}
});
});
</script>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>

select previously selected dropdown value on form self submit

I have a 'this year' dropdown with a form attached. I then select one of the years, like say 2016. When I click the submit button I want the page to be reloaded while the value in the dropdown stays at 2016 (the one that was previously selected).
How can I do that? Here's my form:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){?>
<option value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>
You should use the value from $_POST to set the selected as default. Below is the updated code:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){
$isSelected = (isset($_POST['year']) && ($_POST['year'] == $rows->years)) ? 'selected' : '';
?>
<option <?php echo $isSelected; ?> value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>

PHP If statement in html form

I have the following form but would like to add a condition so that only managers can see 'clear' option.
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<option value='clear'>Remove the ticket</option>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
Something like: if(havePriv('grp_mgr'))
<?php if(havePriv('grp_mgr')) : ?>
<option value='clear'>Remove the ticket</option>
<?php endif; ?>
Does the trick
You can do this:
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<?php if(havePriv('grp_mgr')){ ?>
<option value='clear'>Remove the ticket</option>
<?php } ?>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
So "Remove the ticket" will only displayed if the condition is true.
You can say :
if(havePriv('grp_mgr'))
{
echo "<option value='clear'>Remove the ticket</option>";
}

simplest php dropdown box

I have a simple dropdow box, I want to get the value of the selected item with php. If anyone can help me with it please.
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
---
echo "selected size".$selected;
?>
Provided you put everything in one file:
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
if(isset($_POST['size'])) {
echo "selected size: ".htmlspecialchars($_POST['size']);
}
?>
<select name="rider_select">
<option value="">Select Rider</option>
<?php
foreach ($riderlist as $rows) {
$rname = $rows['name'];
$rnum = $rows['number'];
$rider_full = $rname.'-'.$rnum;
?>
<option value="<?php echo $rname.'-'.$rnum; ?>"
<?= (($rider_fromdb == $rider_full) ? 'selected="selected"' : ''); ?>>
<?php echo $rname.'-'.$rnum; ?></option>
<?php } ?>
</select>

Create a PHP function to display an HTML form?

Instead of repeating myself with my form:
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="$id">
<br>
<input type="submit" value="Lägg till">
</form>
I would like to just call a function and then it would show this form. Could you do that? And for the uID, could i have a parameter to the function?
<?php
function showForm($uid){
?>
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="<?=$uid?>">
<br>
<input type="submit" value="Lägg till">
</form>
<?php
}
EDIT:
If you want this method to return the form you could use output buffering like so:
<?php
function showForm($uid){
ob_start();
?>
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="<?=$uid?>">
<br>
<input type="submit" value="Lägg till">
</form>
<?php
return ob_get_clean();
}
There is an extensive article on this but basically here is the code. https://a1websitepro.com/create-form-php-function/
<?php
function form(){
echo '
<form method="post" action="">
<input type="text"name="firstName"/>
<input type="submit" name="submit" value="submit"/>
</form>
';
}
?>
Or you can use the HEREDOC syntax to put your form into a string within a php function:
<?php
function display_form($uid) {
$str = <<<EOT
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="$uid">
<br>
<input type="submit" value="Lägg till">
</form>
EOT;
return $str;
}
echo display_form(1);
?>

Categories