HTML Forms in PHP - php

i am writing html form code with in php script
here it is
<?php
$problem_code="STR";
echo '<form name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";
?>
But after submitting url look like this localhost/codejudge/submit.php ?
but it should be like this localhost/codejudge/submit.php?id=STR

If a form is method="GET" (which is the default), as this one is, then submitting it will erase the existing query string in the action.
Store the data in a hidden input instead.
<?php
$problem_code="STR";
?>
<form name="submit-button" action="/codejudge/submit.php">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($problem_code); ?>">
<button type="submit">Submit</button>
</form>

You should specify a method of form submit.
$problem_code="STR";
echo '<form method=post name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";

Related

interface php and html, in foreach loop

I have a PHP page that shows me data from the database. I'm inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can't uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?
You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST['unique-id'], also make sure to populate the value of that hidden field using PHP
<?php
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
$numerazione = $_POST["unique-id"];
echo "Unique record is : ".$numerazione."</br>";
}
?>
<form method="POST" action="">
<input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
?>
Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted
<form method="POST" action="">
<input type=hidden name=id value=<?php echo $id; ?>>
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
Solution for this problem:
if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){
echo "Hello world";
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
<input type="submit"
name="mod"
value="valore"
onclick="return confirm('Are you sure?')"
/>
</form>

PHP isset not accepting form name

I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">

Losing GET variable on form submit or on refresh

I am sending an id from a link
<a class="suba" href="empty.php?id=1" ></a>
and recieving this id in the empty.php page :
require_once 'connection.php';
session_start();
$id=$_GET['id'];
on refresh or on form submit the variable $id is lost :
<form action="" method="get">
<input type="submit" name="add" value="next">
</form>
<?php if(isset($_GET['add']) && $_SESSION['j'] < $filecount){
$_SESSION['j']++;
echo '<input type=hidden name=id value=' .$id. '>';
} ?>
I tried adding a hidden input in the form but it's not working also tried saving the variable in a session:
$_SESSION["word"] = $id;
You must print the Id in your form action
<form action="?id=<?= $id ?>" method="get">
<input type="submit" name="add" value="next">
</form>

Passing PHP form variables from one page to other

my php configuration on server are showing i can post variables to maximum size upto 8MB , thats enough .. but how to check for number of variables , sever is running ubuntu 4.4 , php .
i have a page which takes students marks and send them to a action page , but on action page doing echo for the post variables nothing is being displayed , where are doing an echo "hello"; this shows ...
this is the page which sends the variables
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable() ?>
<div class="mainframe">
<input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
and this are the variables are coming to action page .. but on echo they are not showing any value .
$dept =$_COOKIE['dept'];
$join=$_POST['batch'];
$type='e';
$sem=$_POST['sem'];
$chance=$_POST['chance'];
try placing this code on your action page:
if (isset($_GET)) {
echo "<h3>GET METHOD</h3>";
var_dump($_GET);
}
if (isset($_POST)) {
echo "<h3>POST METHOD</h3>";
var_dump($_POST);
}
if (isset($_COOKIE)) {
echo "<h3>COOKIE METHOD</h3>";
var_dump($_COOKIE);
}
See which method returns your variables and use it, otherwise, you are not filling any values on the form.
this is your code:
<form name="frm" action="marklistI.php" method="POST" class="" >
<?php $tb->displayTable(); ?>
<div class="mainframe"> <input type="hidden" name="batch" value="<?php print $_GET['batch']; ?>"/>
<input type="hidden" name="sem" value="<?php print $_GET['sem']; ?>" />
<input type="hidden" name="chance" value="<?php print $_GET['chance']; ?>"/>
<input name="submit" type="submit" class="hide" value="Save"/>
<input type="hidden" name="url" value="<?php print $_SERVER['REQUEST_URI']; ?>"/>
</div>
</form>
One possible reason for your issue:
You use "_GET[]" variables here but the form is POST.
GET and POST are two different methods to send data, GET is in the URL path (a=&b=&c=) while POST is hidden in the HTML headers.
So make sure you read those results as "$_POST['name']" and not GET.
I suggest this in the "receiving script" for debugging:
var_dump($_GET);
var_dump($_POST);
And in your browser use Chrome or Firefox + Firebug and Press "f12".
In that debugger you can catch the POST when you click the button and you can look which variables were sent.
That should help you debug your issue fast.
One other suggestion, I personally would write the code less "mixed".
It makes it hard to read and hard to modify.
Why not like this:
<?php
echo "
<form name='frm' action='marklistI.php' method='POST' class='' >".
$tb->displayTable().
"<div class='mainframe'>
<input type='hidden' name='batch' value='$_GET[batch]'/>
<input type='hidden' name='sem' value='$_GET[sem]' />
<input type='hidden' name='chance' value='$_GET[chance]'/>
<input name='submit' type='submit' class='hide' value='Save'/>
<input type='hidden' name='url' value='$_SERVER[REQUEST_URI]'/>
</div>
</form> ";
?>
My guess for your problem is that those values in the formular are actually empty, that's why you don't receive anything.

sending a value with input get?

<a href="companies.php?id='. $_GET['id'] .'&offset='. $next_offset .'"><input id="button" type="button" value="More"/>
i somehow want to send &offset=avalue but useing a input button. without the id.
how can i do the similer thing with useing form action get ?
like ( warning epic fail ) i should add a hidden input or something ?
echo '<form action="welcome.php" method="get">';
echo '<input id="button" type="button" value="More"/>';
echo '</form>';
please comment if you guys dont understand thanks!
ok somehow i have manage to make it work
echo '<form action="companies.php?id='. $_GET['id'].'" method="get">';
echo '<input type="hidden" name="offset" value="'.$next_offset.'">';
echo '<input id="button" type="submit" value="More"/></a>';
echo '</form>';
but still have an error http://local.host/networks/companies.php?offset=5, where does my get id goes ?
btw im still checking it out and thanks guys :)
and aha! it works
// MORE PLUGIN
echo '<form action="companies.php" method="get">';
echo '<input type="hidden" name="id" value="'.$_GET['id'].'">';
echo '<input type="hidden" name="offset" value="'.$next_offset.'">';
echo '<input id="button" type="submit" value="More"/></a>';
echo '</form>';
// END PLUGIN
For one thing, you can put parameter in the form's action attribute, just like you did with the link and href.
More readable option is hidden input element: <input type="hidden" name="offset" value="your_value">
Is this what you asked?
<? echo '<input type=button onclick=\'window.location="companies.php?offset='. $next_offset .'"\'>';?>

Categories