Loop an array with form - php

i'm working on some code it seems im stuck... anyone can fix the right source code for looping array form?
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name">&nbsp
Stuff Price <input type='text' name='price'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name[] = $_POST['name'];
$price[] = $_POST['price'];
global $jml;
for($i=0;$i<$jml;++$i){
echo $name.' '.$price.'<br>';
}
}
?>
</pre>
i was basic at C++ it's so easy to loop, but i'm still much to learn on this php so anyone can help me please?

What you need to do first is if you're expecting multiple row fields, put a [] in your name attribute in text fields. Example:
<input type="text" name="name[]">
This in turn will accept multiple text inputs under the same name turning it into an array.
Next, since you're reliant on the number of fields to be generated by $jml = $_POST['jml']; this will just be available on the first request. On the next submission this will be gone. Instead of using that with a global which doesn't make sense, just use the count of the submitted text fields.
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name); // get count
Take not that this is reliant to the forms all fields being complete.
After that, its just basic array pointing. echo $name[$i]:
Revised code:
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]">&nbsp
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name);
for($i=0;$i<$count;++$i){
echo $name[$i].' '.$price[$i].'<br>';
}
}
?>
</pre>

Use foreach loop This is how you can easily loop through an array:
foreach($_POST as $key=>$value){
echo $value;
}

Check this
Working code here http://main.xfiddle.com/7ffb488b/stackoverflow/Loopanarraywithform.php
<pre>
<?php
if(isset($_POST['submit'])){
echo "<form action='' method='POST'>";
$jml = intval($_POST['jml']);
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]">&nbsp
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}elseif(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
// global $jml;
$i=0;
foreach ($name as $key => $value) {
echo $value.' '.$price[$key].'<br/>';
$i++;
}
}else{?>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<?php
}
?>
</pre>

Related

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

How to use string from condition1 into condition2?

I have the following code:
<form action='' method='POST' id='form1'>
imdbcode : <input type='text' id='imdbcode' name='imdbcode' /><br/>
<input type='submit' name='submit' value='Get'/>
</form>
<?php
if(isset($_POST['submit'])){
.
.
.
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
//I WANT TO USE $Title from condition1
} ?>
I want to use $title in second condition.
It prints $title in first condition! But doesn't print after closing condition!
How can I do that?
You can't.
Before you can enter the if(isset($_POST['Send']))
you need submit this one:
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
but the moment you submit this, the page will be refreshed and $_POST['submit'] will be deleted, without this variable the $title will not exist.
to fix this or make the $title value alive until the next refresh of the page. you must include the $title value along with the form2.
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value="$title"/>
<input type='submit' name='Send' value='Send'/>
</form>";
and when the user send that form2 you can access the title by using
$_POST['title']
As two your forms are different - there's no connection between them unless you explicitly provide it.
Simple solution can be just echo your $title as a hidden field in a form:
if(isset($_POST['submit'])){
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value='" . $title . "'/>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_POST['title'];
// do other stuff
}
Another solution is to use sessions:
if(isset($_POST['submit'])){
$title = ... ;
$_SESSION['title'] = $title;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_SESSION['title'];
// do other stuff
}
In case of using sessions don't forget to use session_start and probably to unset $_SESSION['title'] in the end of second submit.
<form action='' method='POST' id='form2'>
<input type='hidden' name='text' value='<?php echo $title; ?>
<input type='submit' name='Send' value='Send'/>
</form>
Try to add a hidden input before the Send button.

How can names in an echoed html tags be used

I have a code like this abc.php
I want to create an array of check boxes but if I try accessing the name or the id, it doesn't seem to work.Is there any alternative way to do this?
<html>
<body>
<form method="post">
<?php
echo ""."<input type='checkbox' name='check[]' id='check[]' />"
$c=0;
if(isset($_POST['check[$c]'])){
echo "checked";
}
?>
</form>
</body>
</html>
You have $_POST['check[$c]'] in single quotes, it will not be replaced with the value of $c. Not to mention that multidimensional arrays do not work this way.
To check that $_POST variable treat it as an array:
$checked = $_POST['check'];
// $checked[0] - first element
// $checked[1] - second etc
Use this as a base code, also you can split the php code from the html.
The foreach is better because now you know how what values the user actually submitted in a fast and easy way.
<?php
if(isset($_POST['submit'])){
if(isset($_POST['check']) && is_array($_POST['check'])){
foreach($_POST['check'] as $key => $value){
echo "$key => $value<br>"; # wrap this around single quotes ', see the difference.
}
}
}
?>
<html>
<body>
<form method="post">
<input type='checkbox' name='check[]' value="a">a</input>
<input type='checkbox' name='check[]' value="b">b</input>
<input type='checkbox' name='check[]' value="c">c</input>
<input type='checkbox' name='check[]' value="d">d</input>
<input type='submit' name='submit' id='submit' />
</form>
</body>
</html>
No need to use arra, simply use variable like if(isset($_POST['check'])). If you want to display the value of checkbox then use $_POST['check'][0].
<form method="post">
<?php
echo ""."<input type='checkbox' name='check[]' id='check[]' value='test' />";
echo "<input type='submit' />";
if(isset($_POST['check'])){
echo "checked";
}
?>
</form>

how to use two form's post value together

I have a form on a page like:
<form action='search.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='submit' />
</form>
on search page there is another form like
<form action='' method='POST'>
<input type='submit' name='anygender' />
</form>
then i am using
if(isset($_POST['anygender'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
then $specialist is showing blank and sql query not working..I want to use both form's post value together.Please tell me how to use first form post value in this. Thanks in advance
Why you need two forms?
<form action='index.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='anygender' />
<input type='submit' name='submit' />
</form>
Maybe you can use one form and check buttom?
Use this code. Check $_POST['submit'] in if condition.
if(isset($_POST['submit'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}

How to get the value of the text box outside foreach loop when I click the submit button?

I have a code like this:
I want to know how to get the value of the textbox after i clicked the submit button because on my current code, it only gives me the last $key not all . Thanks
<form>
foreach($array as $key => $values){
echo "<input type='text' name='title' value='$key'/>";
}
<input type='submit' name='submit' value='submit'/>
</form>
<?php
if(isset($_POST[''])){
//get the result of the textbox
$title = $_POST['title'];
}
?>
If you want all the values use array notation, that is put an [] at the end of the element name and you get an array of values.
<form>
foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}
<input type='submit' name='submit' value='submit'/>
</form>
...
<?php
//get the result of the textbox
$titles = $_POST['title'];
foreach ($titles as $title){
echo $title;
}
?>
Your testing for if(isset($_POST[''])){ which will not help your situation. You need to do as follows.
if (isset($_POST['title')) {
$title = $_POST['title'];
}
<form action="" method="post">
<?php
$array=array("a","b","c","d" );
foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}
?>
<input type='submit' name='submit' value='submit'/>
</form>
<?php
if(isset($_POST)){ ECHO "<pre>"; print_r($_POST)}
you should use the name as array
foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}
<?php
//result of the textbox
$titles = $_POST['title'];
print_r($titles);
?>

Categories