HTML Form sending an Array - php

I have a form that looks like the following:
<form action="results.php" method="get">
<input type='checkbox' name='batch[]' value='1'>
<input type='text' name='job_id[]' value='111'>
<br>
<input type='checkbox' name='batch[]' value='1'>
<input type='text' name='job_id[]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
In the example below I have only selected the row with 999 in the textbook.
The results are displayed in the results.php page which code looks like this:
<?php
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($job_id as $key => $value) {
echo $batch[$key]." ";
echo $value."<br>";
}
?>
The above code displays like this:
1 111
999
As you can see the 1 (checkbox) is next to 111. I want to be able to allow send across the job_id from the row selected.
Hopefully I have explained the problem well enough.
Many thanks,
John

add index number in html :
<form action="viewport.php" method="get">
<input type='checkbox' name='batch[1]' value='1'>
<input type='text' name='job_id[1]' value='111'>
<br>
<input type='checkbox' name='batch[2]' value='1'>
<input type='text' name='job_id[2]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
if(isset($_GET['batch'])) {
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($job_id as $key => $value) {
if(isset($batch[$key])) {
echo $batch[$key]." ";
echo $value."<br>";
}
}
}
?>
It will only print:
1 999
because only second check-box is checked.

Give textbox value to checkbox. Same value set to checkbox and textbox.
Javascript
<form action="test5.php" method="get">
<input type='checkbox' name='batch[]' value='111'>
<input type='text' name='job_id[]' value='111'>
<br>
<input type='checkbox' name='batch[]' value='999'>
<input type='text' name='job_id[]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
PHP
<?php
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($batch as $key => $value) {
echo $value."<br>";
}
?>

Related

Get input type="text" values by checked checkboxes

I'm doing a little project and want to get all input type="text" values if the checkbox is checked. How does it work?
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_blank">
<?php
foreach ($data['items'] as $key => $value) {
echo "<label class=\"Items\">
<input type=\"checkbox\" checked=\"checked\" name=\"items[]\" value=\"$value\">
<input type=\"text\" value=\"$value\">
</label>";
}
?>
</form>
Ive already tried this, but it only returned my checkbox values not my text values.
if(isset($_POST['items'])) {
$items = $_POST['items'];
}
First of all, to pass textbox values to server, you also need to give the textbox a name:
<input type="text" name="values[]" value="$value">
One way to sync your textboxes with your checkboxes is this small hack:
foreach ($data['items'] as $key => $value) {
echo "<label class='Items'>
<input type='hidden' name='keys[{$key}]' value='checkbox_isnt_selected'>
<input type='checkbox' checked='checked' name='keys[$key]' value='$value'>
<input type="text" name='values[$key]' value='$value'>
</label>";
}
This way, if the checkbox isn't selected, you get checkbox_isnt_selected on your server, otherwise, you get the relevant $value. The checkbox will overwrite the hidden input's value when checked.
If you want to use $_POST['items'] to access all data from your form, do it like so :
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_blank">
<?php
foreach ($data['items'] as $key => $value) {
echo "<label class='Items'>
<input type='checkbox' checked='checked' name='items[$key][]' value='$value'>
<input type='text' name='items[$key][]' value='$value'>
</label>";
}
?>
</form>

is it possible to add a variable inside the isset $_post['submit']

Is it possible to do something like this?
if (isset($_POST['Submit_'.$_POST['ID']])) {
}
or
if (isset($_POST['Submit_' + $_POST['ID']])) {
}
I want to do this so i can get a different submit button for all the posts, because i got a comment system, inside a posts system.. so on all the posts its a new submit button.
submitbutton is made like this:
echo "<button type='submit' class='commentbtn' name='commentSubmit_".$row['ID']."'></button>
i have checked that all the buttons gets a different name, so its just the other thing i dont get to work.
comment form:
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button></form></div>";
setComment Function:
function setComment($conn) {
if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
$uname = $_POST['uname'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$comment = $_POST['commentText'];
$uimg = $_POST['uimg'];
$sid = $_POST['sid'];
$sql = "INSERT INTO status_comments (uid, sid, uname, comment, uimg, date) VALUES ('$uid', '$sid', '$uname', '$comment', '$uimg', '$date')";
$result = mysqli_query($conn, $sql);
}
}
complete setStatus Function with comment form inside:
function getStatus($conn) {
$sql = "SELECT * FROM status ORDER BY sid DESC";
$query = mysqli_query($conn, $sql);
while ($row1 = $query->fetch_assoc()) {
echo "<div class='commentbox'>";
echo "<div class='commentimg'><img src='images/".$row1['commentimg']."' width='50px'></div>";
echo "<div class='namedate'>";
echo $row1['uidname']."<br>";
echo "<div class='commdate'>".$row1['date']."<br><br></div></div><hr>";
echo "<div class='statusmessage'><p>".nl2br($row1['message'])."</p></div>";
echo "<div class='statusimage'><img src='userimages/".$row1['status_image']."'></div>";
echo "<div class='likerow'>";
$result = mysqli_query($conn, "SELECT * FROM status_like WHERE uid=".$_SESSION['id']." and sid=".$row1['sid']."");
if (mysqli_num_rows($result) == 1) {
echo "<span><a href='' class='unlike' id='".$row1['sid']."'><div class='unlike-btn'><img src='images/dislike.png'></div></a><p>| ".$row1['likes']." likes!</p></span>";
} else {
echo "<span><a href='' class='like' id='".$row1['sid']."'><div class='like-btn'><img src='images/like.png'></div></a><p>| ".$row1['likes']." likes!</p></span>";
}
echo "</div>";
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button></form></div>";
if ($_SESSION['id'] === $row1['uid'] || $_SESSION['usertype'] === 'topadmin'){
echo "<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button type='submit' title='Delete Status' name='statusDelete'>X</button>
</form>
<form class='edit-form' method='POST' action='editcomment.php'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<input type='hidden' name='uid' value='".$row1['uid']."'>
<input type='hidden' name='date' value='".$row1['date']."'>
<input type='hidden' name='message' value='".$row1['message']."'>
<button type='submit' style='border: 0; background: transparent'>
<img src='images/edit-icon.png' height='10'' alt='edit' title='Edit Status'' />
</button>
</form>";
}
echo "</div>";
}
}
Yes your first expression is correct. Check PHP concatenate for more information. Though I would use following to check form submit.
// check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// .... your code
}
Additionally, I have had look on your code. I think the error is in form HTML.
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
It cause function to run 4 times and recording coment 4 times. Please replace action='#' or action='processComent.php' // location of process code.
Do the same in
<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>
also as same thing will happen while deleting.
Yes, it's possible and your first variant is correct.
Thought a better idea might be make a separate form for each comment field and add the ID as a hidden input.
Something like this:
<form method="post" action="/something">
<textarea name="comment">
<input type="hidden" name="id" value="$row['ID']">
<button type="submit" name="submit"></button>
</form>
I can't really follow the logic in your code, but it seems as if you are over complicating the process... and i assume that your action was not actually pointing to that function as you show, but instead, is pointing to a page with that function ... anyway ... if you have this form
echo "
<div class='commentform'>
<form id='comment_form_".$row1['sid']."' action='setcomment.php' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button>
</form>
</div>";
and then when that form posts to setcomment.php, something simple like this would work ...
if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
setComment($conn);
}
function setComment($conn) {
$uname = $_POST['uname'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$comment = $_POST['commentText'];
$uimg = $_POST['uimg'];
$sid = $_POST['sid'];
$sql = "
INSERT INTO
status_comments
(
uid, sid, uname, comment, uimg, date
)
VALUES
(
'".mysqli_real_escape_string($conn,$uid)."',
'".mysqli_real_escape_string($conn,$sid)."',
'".mysqli_real_escape_string($conn,$uname)."',
'".mysqli_real_escape_string($conn,$comment)."',
'".mysqli_real_escape_string($conn,$uimg)."',
'".mysqli_real_escape_string($conn,$date)."'
)";
$result = mysqli_query($conn, $sql);
}

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.

Loop an array with form

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>

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>";
}
}

Categories