Read value from user via html ,php ,mysql - php

I should create a database table and for this thanks to php and html I get number of fields and fields name from the user.And I can not handle it I try to fill toKeep array but I can't do it.My goal is keep fields in toKeep and then create tables.
Please enter the number of fields
<form name="getNumber" action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="number" >
<input type="submit" name="field" id="field">
</form>
<?php
$number=$_POST["number"];
$num=(int)$number;
$i=1;
$toKeep = array();
while ($i<=$num){
echo "please enter fieldname";
$fieldname = "field" . $i;
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="<?php echo htmlspecialchars($fieldname);?>">
</form>
<?php
$toKeep[i]=$_POST['$fieldname'];
//echo $fieldname;
echo $toKeep[i];
$i=$i+1;
}
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="submit" name="field" id="field">
<?php
if(isset($_POST['field'])){
echo $toKeep[0];
echo $toKeep[1];
}
?>

You have 2 typos there .
You need to change
$toKeep[i]=$_POST['$fieldname'];
to
$toKeep[$i]=$_POST[$fieldname];

Related

Submit form value return

I have this source code...
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
In the following code I get the values for 'note'...
if (isset($_POST["submit_eingabemaskeR"]))
{
echo ("<br/>");
//Note
echo $_POST["note"];
But how can I echo the value of the first field -> SpielerName?
SpielerName is not a form field, its just text.
If you want its data submitted you can make a hidden form field with that value.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo htmlspecialchars($SpielerName); ?>
<input type="hidden name="SpielerName" value="<?php echo htmlspecialchars($SpielerName); ?>">
<br>Note: <input type="text" name="note" value=<?php echo htmlspecialchars($Note) ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo htmlspecialchars($Tore) ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
Use a hidden input.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<input type="hidden" name="SpielerName" value=<?php echo $SpielerName; ?> >
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">

How can I keep the URL id on submit?

When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.

connection between php scripts and html input

<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" " />
</form>
I want to make that value in php script ... be related to the value of the input form html
I tried so many ways ,but not implemented , please help
Try it like
PHP in HTML :
<input type="text" class="span3" name="dateField1" value="<?php echo $dateField1->makeDateField();?>" />
Or even you can try like
HTML in PHP :
<?php echo '<input type="text" class="span3" name="dateField1" value="'. $dateField1->makeDateField().'" />';?>
Try this:
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
$value = $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" <?php echo $value; ?> " />
</form>
You can use php inside html by starting php tags within html
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value="<?php echo $value;?>" />
</form>

why php input blank value or duplicate values in one form if submit another form

I am encountering a strange phenomena: When I submit a form in my code, then beside doing what is there in that form, it looks like it also trigger the comment form. So when the page is reloading, I get some blank comments or duplicate comments. In my code I have several forms with submit, and one of that is the form for input comment:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" />
</form>
Could any of you point out for me where it gets wrong?
As requested, I post the whole (updated) code here:
<h3>Comments</h3>
<!-- <p>Put your comments here: </p> -->
<?php
//a: commenters
$i = addslashes($_POST['a']);
$ip = addslashes($_POST['b']);
$a = addslashes($_POST['c']);
$b = addslashes($_POST['d']);
if(isset($_POST['form1'])){
if(isset($i) & isset($ip) & isset($a) & isset($b))
{
$connector= new DbConnector();
$r = mysql_query("SELECT COUNT(*) FROM `databasename`.`ban` WHERE ip=$ip"); //Check if banned
$r = mysql_fetch_array($r);
if(!$r[0]) //Phew, not banned
{ // echo "a: ".$a." b: ".$b." ip: ".$ip." i: ".$i."";
$Date4=date('Y-m-d H:i:s');
if(mysql_query("INSERT INTO `databasename`.`Comments` VALUES ('$a', '$b', '$ip', '$Date4')"))
{
?>
<script type="text/javascript">
window.location="/index.php?id=".<?php echo $i; ?>;
</script>
<?php
}
else echo "Error, in mysql query";
}
else echo "Error, You are banned.";
}
}
$x = mysql_query("SELECT * FROM `databasename`.`Comments` ORDER BY i DESC ");
while($r = mysql_fetch_object($x)) echo "<div class='c'>".$r->a."<p>".$r->b."</p> </div>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" name="form1" />
</form>
You need use isset()
in HTML submit button you need to use name attribute for each and every form,
<input type="submit" name="form1" />
IN PHP,
<?php
if(isset($_POST['form1']){
echo $_POST['a'];
}
?>
Your code,
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id']; ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d"></textarea>
<input type="submit" name="form1" />
</form>
I would use:
if(!empty($_POST['form1']){
echo $_POST['a'];
}
empty() will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
Read more: http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

TwoPHP blocks in file

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>
Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>
You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

Categories