Get value of input in table and then save in mySql database - javascript

I have seen some answers but I have not found anything that I want. I have a table with these rows:
while ($rown = mysql_fetch_assoc($resulti)){
echo "<tr style=text-align:center>";
echo "<td>".$rown['emri']."</td>";
echo "<td>".$rown['mbiemri']."</td>";
echo "<td>".$rown['dega']."</td>";
echo "<td>".$rown['id']."</td>";
echo "<td>".$rown['viti_shkollor']."</td>";
echo "<td><input type=text style= width:30px />"</td>";
echo "</tr>";
}
It works perfectly but I want to get the values of input type that user enters and then to put them in a mySql database. Also the table has a submit button as below so when the user completes all of the table inputs then press the submit and all of values go to the database.
<input type="submit" value="prano" name="prano" style="margin:0 auto;" />
It is difficult for me to get the value of input and to put it in a variable. I hope you understand my question. Please help me.

there are few points which you need to include:
1) add form in to your html
2) add name to all input box with in while
echo '<form action="test.php" method="post">';
while ($rown = mysql_fetch_assoc($resulti)){
echo "<tr style=text-align:center>";
echo "<td>".$rown['emri']."</td>";
echo "<td>".$rown['mbiemri']."</td>";
echo "<td>".$rown['dega']."</td>";
echo "<td>".$rown['id']."</td>";
echo "<td>".$rown['viti_shkollor']."</td>";
echo "<td><input name='custom[".$rown['id']."]' type=text style= width:30px />"</td>";
echo "</tr>";
}
echo "</form>";
So that now in test.php:
in $_POST[custom] you will get array of values of all input field custom.

Related

Javascript Only the first button submits to Ajax form despite different Ids

Other users have had similar problems about when they're looping rows of buttons but it's always because they accidentally reused the same Id or value. I'm experiencing the same problem, but all of my buttons are unique.
AJAX request
<script>
$(document).ready(function(){
$("#friendadd").submit(function(){
alert("checkpoint");
$.ajax({
type:"POST",
url:"getuser.php"
});
})
});
</script>
PHP and form
<form id="friendadd">
<?php
for($i=0; $i<$ctk->rowCount(); $i++){
echo "<img src='".$ctk_values[$i][6]."' alt='Blank' style='width:64px;height:64px'>";//PP, Later add clickable profile
echo "<th rowspan='3'>Attributes</th>";
echo "<tr> ".$ctk_values[$i][0]."</tr>";//UN
echo "<tr> ".$ctk_values[$i][1]."</tr>";//UL
echo "<tr> ".$ctk_values[$i][5]."</tr>";//UA
?>
<input type="submit" id="friend<?php echo $i;?>"><!--pass in this.value-->
</form>
<?php
}//Ends for loop
}
}
?>
Explanation: When I type in a username into the search box, it returns me three different users named rikesh1, rikesh2, and rikesh3. Each of them have a button next to them, with values friend0, friend1, friend2, respectively. When I click on the friend0 button, it successfully calls and updates the database. When I click the friend1 button, nothing happens. This is different from other users in that my buttons have unique Ids. Thanks for any and all help, I think this is a very fixable problem but after searching Stack, I'm still not sure what's happening.
<form id="friendadd">
<?php
for($i=0; $i<$ctk->rowCount(); $i++){
echo "<img src='".$ctk_values[$i][6]."' alt='Blank' style='width:64px;height:64px'>";//PP, Later add clickable profile
echo "<th rowspan='3'>Attributes</th>";
echo "<tr> ".$ctk_values[$i][0]."</tr>";//UN
echo "<tr> ".$ctk_values[$i][1]."</tr>";//UL
echo "<tr> ".$ctk_values[$i][5]."</tr>";//UA
?>
<input type="submit" id="friend<?php echo $i;?>"><!--pass in this.value-->
<?php
}//Ends for loop
?>
</form>
<?php
}
}
?>
Use this code instead.
The other one ends the form tag at the first loop.

How do I get button's ID from a list of buttons?

I haven't worked much with php and javascript and was hoping to get some help. I want to be able to detect which button was clicked exactly. I'm creating one button per row but I don't know how to figure out what row the click was made in. This is what I have so far...
# Get all the table data from the database
$getData = mysqli_query($conn, "SELECT * FROM `query_table`") or die ("Error occured while selecting!");
// echo '<form method="post" action="index.php">';
echo '<table border="1">';
echo '<tr>';
echo '<td>Questions</td>';
echo '<td>Likes</td>';
echo '<td></td>';
echo '</tr>';
# Loop through each row and print out the question
while ($row = mysqli_fetch_array($getData)) {
echo '<tr>';
echo '<td>';
echo $row['Question'];
echo '</td>';
echo '<td>';
echo $row['Likes'];
echo '</td>';
echo '<td>';
?>
<input type="button" id="myButton" value="Like" onclick="myFunc(<?=$row['ID']?>)" />
<?php
echo '</td>';
echo '</tr>';
}
echo '</table>';
// echo '</form>';
echo '<p id="text"></p>';
mysqli_close($conn);
?>
<script type="text/javascript">
function myFunc(id) {
document.getElementById("text").innerHTML = id;
}
</script>
So far this prints out the row id where the button was clicked, but I don't suppose I can assign that parameter value directly to a php variable that I can then use for mysql queries. How is stuff like this normally done, please help.
<input type="button" data-id="<?=$row['ID']?>" value="Like" onclick="myFunc(this)">
and
<script type="text/javascript">
function myFunc(button) {
document.getElementById("text").innerHTML = button.getAttribute('data-id');
}
</script>
In JavaScript the token "this" refers to the context in which the code is running which in this case is the button. Store custom data in attributes prefixed with data- to ensure they are not disturbed - standard practise.

PHP POST and Javascript?

This may sound confusing, but I am unsure as where to start looking for an answer. This is the scenario: I have a webpage with a table created using PHP and inside each cell is a randomly selected word. What I would like to do is allow a user to click one of the cells and it would return the definition of the word, and refresh the table/page. From what I found so far was to make use of _POST/_REQUEST, however I am unsure how to find out what the user clicked, and pass that into a function to find the definition. Is my logic correct here, how would you go about this? I was thinking of having an onclick function to identify the element clicked, but don't know how to handle it.
<body>
<form method="post" action="
<table border="1">
<?php
$f="/words.txt"; //definitions also included in this file
$o=file($f);
$len=count($o);
$i=0;
while( $i < 18){
$rnum= rand(2,$len);
$rword= $o[$rnum];
$piece= explode(" ",$rword); //get just the word on the line
if($i%3==0){
echo "<tr>";
}
echo "<td id='$i' onclick='about()'>".$piece[2]."</td>";
$i++;
if($i%3==0){
echo "</tr>";
}
}
?>
</table>
</body>
</html>
The simplest thing you may done without need to any javascript is to make number forms equals to the number of cells you have. It is something like the following:
//Remove the form tag
<table border="1">
<?php
$f="/words.txt"; //definitions also included in this file
$o=file($f);
$len=count($o);
$i=0;
while( $i < 18){
$rnum= rand(2,$len);
$rword= $o[$rnum];
$piece= explode(" ",$rword); //get just the word on the line
if($i%3==0){
echo "<tr>";
}
?>
<td id='<?php echo $i; ?>'><form method="post"><input type="hidden" name="word" value="<?php echo $piece[2];?> /><input type="submit" value="<?php echo $piece[2];?> /></form></td>;
<?php
$i++;
if($i%3==0){
echo "</tr>";
}
}
?>
</table>
By this way you have a submit button for each word you have that submit its own form's hidden element with the name word to be precessed on the server side.
I suggest you to create a form that contains your word. Then you can simply specify action=”yourpagewithresponse.php” onclick="submit()" in the attributes of the form. In this way you don't need the submit button, you can simply click on the word. Obviously you can iterates the form creation in php language for having more word containers.
<form method="post" action=”yourpagewithresponse.php” onclick="submit()"><input type="hidden" name="word" value="<?php echo $piece[2];?>" /><?php echo $piece[2];?></form>

two different submit button on a form codeigniter acting weird

I have two buttons on one submit form but when I post the submit button it returns nothing;"ABOUT THE BUTTON"; the other data is retrieved WELL.
I have been using different names for the submit button but nothing works.. Could someone help me? thank you.
The FORM data is retrieved well but not the submit button value? i need the value to differ the condition
controller: admin.php
function submit_unitgroupwd(){
$this->access_chk(); //CHECK USER ACCESS
$sbm=$this->input->post('sbm'); // returns NOTHING
echo $_POST["sbm"]; // returns undefined index sbm
echo $_POST['sbm']; // returns undefined index sbm
var_dump($_POST); // returns all the post data bt the submit button
//echo var_dump($this->input->post("sbm"));
exit();
if($this->input->post('sbm') == "Update") {
// do update
} else {
// do delete
}
}
view:
echo "<form action=".site_url("admin/submit_unitgroupwd")." method=post id=form>";
echo "<div style='float:left;color:red;' id='res'></div>";
?><?php
foreach($q1->result_array() as $r1){
$bankname= substr($r1["bank"],0,3);
//echo $bankname;
echo "<input type=hidden name=dgroup value='".$dgroup."'>";
echo "<tr>";
echo "<td width=20% colspan=3 style='font-size: 16px;'>".strtoupper($r1["bank"])."</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=100>".$group_[0]."</td>";
echo "<td><input type=text name='$bankname-utama' value='".str_replace(" ","",$r1["utama"])."' style='width: 100%;'></td>";
if ($r1["bank"] == 'ALL'){
echo "<td rowspan='1'><br><input type='checkbox' name='cek[]' id='cek[]' value='$bankname' ></td>";
echo "<input type=hidden name=_code$bankname value='".$r1["bank"]."'>";
echo "</tr>";
echo "</tr>";
continue;
}
echo "<td rowspan=".($dgroup + 1)."><br><input type='checkbox' name='cek[]' id='cek[]' value='$bankname' ><br><br><br><br><br><br><br>";
echo "<input type=hidden name=_code$bankname value='".$r1["bank"]."'>";
echo "</tr>";
$_alp = "a";
for ($_g=0; $_g<$dgroup; $_g++){
echo "<tr>";
echo "<td width=100>".$group_[($_g + 1)]."</td>";
echo "<td width=80%><input type=text name='$bankname-wd$_alp' value='".str_replace(" ","",$r1["wd$_alp"])."' style='width: 100%;'></td>";
echo "</tr>";
$_alp++;
echo "</tr>";
}
}
?>
>
<input type="submit" class="button" value='Update' name="sbm"/>
<input type="submit" class="button" value='Delete' name="sbm"/>
I'm not completely clear on your description of the problem, but you can't name both submit buttons the same.
You seem to think that push one or the other button will only submit THAT button's value, but in fact the second one is always submitted, because it is the last on the form.
So change your button names to "update" & "delete", and then you can check with
if(isset($_POST['update']))....
for example.
However, you might want to look around the internet for some cleaner examples of how to code what you are after.
You could simply use only the
<input type="submit" class="button" value='Update' name="sbm"/>
and use an anchor for the delete action,
echo "<a href ='controller_delete/$item_id' class='button'>Delete</a>
since you normally wont need other inputs on the delete action if you get my point.

how to get the values from form which was send using javascript

I want to get the values from a form which was send to the page using javascript.
here is my javascript function and form.
<?php
echo"<html>";
echo"<script type='text/javascript'>";
echo"function submitform(){";
echo"document.getElementById('myform').submit();}";
echo"</script>";
echo"<form id='myform' action='comment.php' method='post'>";
echo"<tr>";
echo "<td><img src='photos/$file' alt='$file'><br></td>";
echo "<tr><td><a href=\"delete.php?id=".$file."\" onclick=\"return confirm('You want to delete .$file???');\">".$file."</td></tr>";
echo "<tr><td><a href=\"like.php?id=".$file."\">like</td></tr>";
echo "<tr><td>".$result."likes</td><tr>";
echo "<tr><td><a href=\"dislike.php?id=".$file."\">unlike</td></tr>";
echo "<tr><td>".$result1."unlikes</td><tr>";
echo"<tr><td colspan='2'>Your Comment: </td>";
$_SESSION['comment']=$file;
echo" <td colspan='5'><textarea name='comment' id='comment' rows='2' cols='25' ></textarea></td></tr>";
echo"<tr><td colspan='2'><a href='javascript: submitform()'>Comment</a></td></tr>";
//echo"<td><form name='up' action='delete.php' method='post' enctype='multipart/form-data></td>";
//echo"<td><a href='http://127.0.0.1/delete.php'><button name='button' type='submit' value='photos'>delete</button></a></td>";
//$_SESSION['comment']=$file;
echo"</form>";
echo"</tr>";
?>
In the comment.php file the values are available in the $_POST array.
$_POST['comment'] // -> what the user wrote in the textarea.
first in comment.php use print_r($_POST) and check data is get or not.
and you use post method to send data so you can get value using $_POST['field_name'].
and try to make html,javascript and php code like this:
<html>
<script type='text/javascript'>
function submitform(){
document.getElementById('myform').submit();
}
</script>
<form id='myform' action='comment.php' method='post'>
<tr>
<td><img src='photos/<?php echo $file;?>' alt='<?php echo $file;?>'><br></td>
:
:
</tr>
</form>
</html>

Categories

Resources