Get all form elements and irregular characters - php

I have a form that can have any number of elements. Each element has a few characteristics. How can i get all the form's data into a url for ajax posting?
the variable id is the id of the given item.
Item 1
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
Item 2
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
Item 3
<input type='text' name='<?php echo $id . "['name']";?>' id='i_<?php echo $id . "['name']";?>'>
<input type='text' name='<?php echo $id . "['size']";?>' id='i_<?php echo $id . "['size']";?>'>
if i do
var inputs = $('#formName :input');
var data = "";
inputs.each(function() {
data += this.name+"="+$(this).val()+"&";
});
the brackets in the identifier mess everything all up. I know im am going about this incorrectly. What should i be doing?
if i do serialize the same thing occurs. I know its not ideal to use brackets in a element id, but how else can i associate a few characteristics of each item to the id?
Here is my rendered form:
<form name='formName' id='formName' action='#'>
<ul id="sortable">
<li class="ui-state-default" id='201817'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='201817['name']' id='a_201817['name']' value='hat'>
<input type='text' name='201817['size']' id='a_201817['size']' value='small'>
</li>
<li class="ui-state-default" id='501817'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='501817['name']' id='a_501817['name']' value='shirt'>
<input type='text' name='501817['size']' id='a_501817['size']' value='small'>
</li>
<li class="ui-state-default" id='522227'>
<input type='button' class='removeItem' value='remove'>
<input type='text' name='522227['name']' id='a_522227['name']' value='shirtB'>
<input type='text' name='522227['size']' id='a_522227['size']' value='Large'>
</li>
</ul>
</form>

You can use form.serialize it will consider all :input fields. Also remember that serialize will consider the element name and not the id.
So Try:
var data = $('#formName').serialize();

Related

change to php 8 some POST data after HTML form submit is empty [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 days ago.
I pass data from a form (once hidden and once by input) by click to a function. But unfortunately the data on the hidden input field is given to the $_POST
<?php>
$abschlag1 = "gelb";
$ndbw01 = 8;
echo "<form name='Formular' action='' method='post' >";
echo "<td align=center><input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2'>" . $ndbw01 . "</td>";
echo "<input type='hidden' id='abschlag1' name='abschlag1' value=$abschlag1>";
echo "<input type='submit' name='submit1' value='Speichern' onclick='chkFormular();'/>";
echo "</form>";
?>
<script type="text/javascript">
function chkFormular() {
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
}
</script>
The result ist:
$_POST['abschlag1‘] => gelb
$_POST['L01w‘] => empty
I hope, it is now a little clearer.
You can't run a PHP script on a JavaScript event directly. PHP usually is executed at page generation on the server side but if really needed you can run a PHP script on a JavaScript event using Ajax for example.
In order to help you, correct me if I'm wrong, but I think what you want to do is having your fields still completed with the sent values after the form is submitted. You can do it like this using:
value='<?php if isset($_POST["field"]) echo $_POST["field"]; ?>'
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?php if isset($_POST['L01w']) echo $_POST['L01w']; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?php if isset($_POST['abschlag1']) echo $_POST['abschlag1']; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>
If you still want to keep your data in variables, you update your code to this:
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?= $L01w; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?= $abschlag1; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>

passing while loop value to another form

In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">
I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...

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

Post variables from a second table in form

I'm trying post 3 variables in order to insert them in table2. As you can see I'm using a SELECT to get 3 variables from table1 to insert into a table2 but can't see the input value when looking at the source.
The loop works. I get results in the loop but the input post echo's nothing. I've tried many different ways but can't seem to get it to work. Can someone help?
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
?>
<input type="hidden" name="name" id="name" value="<?php echo "{$_POST['name']}"; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo "{$_POST['cover']}"; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo "{$_POST['pageno']}"; ?>">
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
I don't see why there are two queries. You can just drop the first one. Also, I'd like to suggest you this-
Instead of echoing like this
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
echo it like this-
while($row = mysqli_fetch_array($result))
{
echo "<input type='text' value='" . $row[name] . "' name='name' id='name'>";
echo "<input type='text' value='" . $row[cover] . "' name='cover' id='cover'>";
echo "<input type='text' value='" . $row[pageno] . "' name='pageno' id='pageno'>";
}
In your last bit of code, please make these small changes-
<input type="hidden" name="name" id="name" value="<?php echo $_POST['name']; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo $_POST['cover']; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo $_POST['pageno']; ?>">
Please clarify- why would you echo input controls in a loop? You could potentially get hundreds of input controls rendered on your page. Now you don't want that do you? Hope my answer helps.

Categories