I am new in HTML
i want to make a user list web page. The page reads user.txt and show then left column of the tabel.
And new items in which New User column will be added to user.txt list
Instead of get/post http methos i am trying to use 2 AJAX methods to communicate the server side.
After sending url with AJAX i will call php scripts on the server side
I think first AJAX method for adding is below
XMLHttpRequestObject.open("GET", dataSource);
However. I coulnt find the second. And i dont know how to implement
Here is my code
<!DOCTYPE html>
<SCRIPT language="javascript">
function add(nm, ml) { }
function read(nam,mail) { }
</SCRIPT>
<html>
<body>
<table border = "1">
<thead>
<tbody>
<tr>
<th>Registered Users</th>
<th>New User</th>
</tr>
<tr>
<td></br></td>
<td>
<p><label>Name:
<input name = "name" type = "text">
<p><label>Email:
<input name = "email" type = "text">
</td>
</tr>
<tr>
<td>
<p></p>
<button onclick="read()">Read users list</button>
</td>
<td>
<p></p>
<button onclick="add(name,email)">Add user to list</button>
<p id="demo2"></p>
</td>
</tr>
</tbody>
</thead>
</table>
</body>
</html>
Related
I tried to look in this site but none of the question which relate exactly like mine. I need help on this problem since am very new in PHP.
Iam developing students result application which used to collect scores of the students. My problem is, I fail to submit all information I get like (idnumber, names and subject), after searching records of students of a certain class, here is my scripts.
<?php
$number = $courseObj->sele_ct_class($class, $year);
?>
<table class="table table-bordered" style="border-radius: 100px" border="0">
<tr style="font-size: medium; background-color: lavenderblush">
<td>#</td>
<td>Admission Number</td>
<td>Student name</td>
<td>Max.Mark</td>
<td>Mark</td>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$notaVilableId = $courseObj->seleId($class, $year); // Select_all From curClass according to aguments (return all idnumber)
foreach ($notaVilableId AS $haika) {
$classIdnumber = $haika->idnumber;
$jina = $haika->jina;
?>
<tr>
<td style="width: 5%"><?php echo $i++; ?></td>
<td><?php echo $classIdnumber ?></td> <!-- this display idnumber -->
<td><?php echo $jina ?></td> <!-- this display name -->
<td>100</td>
<td class="col-sm-2"><input type="number" id="subject" name="subject" class="form-control"></td> <!-- this fiels used to input score marks -->
</tr>
<?php } ?> <!-- end foreach -->
<tr>
<td colspan="20px">
<button class="btn btn-success" type="submit" name="fee_submit" id="fee_submit">Save
</button> <!-- submission button -->
</tr>
Problem is here, I get only idnumber of one student and the rest not appear, can any one show me how I could get all information like (idnumber, name and subject score which entered for each student). I mean that I want all information displayed to be submited with there scrored marks. Thank
<?php
if (isset($_POST['fee_submit'])) {
$classIdnumber;
$jina;
$score = sanitize($_POST['subject']);
$courseObj->insertseTExams($classIdnumber, $jina, $score); // function used to insert data
}
change this
<input type="number" id="subject" name="subject" class="form-control">
to note name="subject[]" change
<input type="number" id="subject" name="subject[]" class="form-control">
getting the data in php
i=0;
$number=count($_POST["subject"];
while ($i < $number ){
$score = sanitize($_POST['subject'][$i]);
//instert your data or do whatever else
$i++;
}
it seems you have assigned $classIdnumber,$jina to read its values from the $_POST super global variable.. And also, any data that is to be submitted to a server page for processing should be in-between form tags,this way you can access this values through the $_POST super global
I have html table generated via ajax. And last column on this table contains button. My question is what is the best practice to submit these rows (only one at time. I need use this method to amend records).
Is it worth to wrap each row with
<form>
<input type="hidden" value="hidden value">
<input type="submit">
</form>
Or people using something difference? Reason why i'm asking for is because i'm worry about very long list example 1k rows or 10k rows (that means i will have 1k or 10k forms on a page).
You can just use a hyperlink (which you can style to look like a button using CSS if you want). e.g:
Edit
where the value you give as the "id" parameter is the primary key of the record in that row.
Then in edit.php look for the id value using $_GET["id"] and fetch the appropriate record from the DB.
As Progrock advises, a form element may only be used "where flow content is expected" (i.e. not as a direct child of table or tr).
HTML 5 introduces a form attribute as a workaround:
<form id="row_1">
<input type="hidden" name="id" value="pk1">
</form>
<form id="row_2">
<input type="hidden" name="id" value="pk2">
</form>
<table>
<tr>
<td> <input type="text" name="attribute1" form="row_1"> </td>
<td> <input type="submit" form="row_1"> </td>
</tr>
<!-- and so on for each row -->
</table>
It has been brought to my attention that in this case, there is no direct user input being submitted, but only generated contents.
Well, then the solution is even simpler:
<table>
<tr> <td>
<form id="row_1">
<input type="hidden" name="id" value="pk1">
<input type="hidden" name="attribute1" value="whatever">
<input type="submit">
</form>
</td> </tr>
<!-- and so on for each row -->
</table>
I thought I'd have a go without form elements, working with editable table cells. Within each row you provide a button. And when you click it, an ajax post is made of the cell values.
You could have a non js fall back where the save button is replaced for an edit button that takes you to another page with a single form.
Forgive my JS.
I have the session storage in there just to check the concept.
<?php
session_start();
var_dump($_SESSION);
$data = array(
23 => ['triangle', 'green', '2'],
47 => ['square', 'red', '3'],
17 => ['pentagon', 'pink', '4']
);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Save state here
$_SESSION['submission'] = $_POST;
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(function() {
$('button').click(function() {
// Get all table cells in the buttons row
var $cells = $(this).closest('tr').find('td[contenteditable="true"]');
var jsonData = {};
$.each($cells, function() {
jsonData[get_table_cell_column_class($(this))] = $(this).text().trim();
});
jsonData['id'] = $(this).attr('id');
$.post('',jsonData, function() {
alert('Saved.');
});
});
function get_table_cell_column_class($td)
{
var $th = $td.closest('table').find('th').eq($td.index());
return $th.attr('class');
}
});
</script>
</head>
<body>
<table>
<thead>
<tr>
<th class="shape">Shape</th>
<th class="colour">Colour</th>
<th class="width">Width</th>
<th>Ops</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $key => $row) { ?>
<tr>
<?php foreach($row as $field) { ?>
<td contenteditable=true>
<?php echo $field ?>
</td>
<?php } ?>
<td>
<button id="<?php echo $key ?>">Save</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
You can use the following
<table id="YourTableId">
...
<tr data-id="yourrowId">
<td class="col1"> value1</td>
<td class="col2"> value2</td>
<td class="col3"> value3</td>
<td class="actions">
Submit
</td>
</tr>
....
</table>
your javascript code will be like
$(document).ready(function (){
$('#YourTableId a').off('click').on('click',function(e){
e.preventDefault();
var tr = $(this).closest('tr')
var data={ // here you can add as much as you want from variables
'id' : tr.data('id), // if you want to send id value
'col1': tr.find('.col1').text(),
'col2': tr.find('.col2').text(),
'col3': tr.find('.col3').text(),
};
$.ajax({
method: 'post',
url: 'your url goes here',
data: data,
success: function(result){
// handle the result here
}
});
});
});
Hope this will help you
So I have this snippet of code that i want to insert into my site.
<table border="1">
<col width="130">
<col width="80">
<tr>
<td align = "right">Steps:</td><td align="center">Add as many steps as you need</td>
</tr>
<tr>
<td align = "right">Step 1:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 2:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<!--<tr>
<td align = "right">Step 3:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 4:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
-->
</table>
For the most part, I have some commented out since I am testing to make sure that my site can handle multiple text boxs but the problem that I have is how can I insert steps into my site using php? I have these all on .php files in case you are wondering but I would like to insert more boxes with a button that is just under the current boxes. Every time the user clicks it, it should insert another box underneath the current boxes and update the number accordingly. I just am not sure where to start and how to get them in. Any ideas?
You would create new elements in JS and jQuery using the following code:
HTML
<button id="addButton">Add me!</button>
JS
var stepCounter = 3; //Number of the first step to be added
$('#addButton').click(function () {
var tr = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var textarea = document.createElement("textarea");
$(textarea).attr("name", "steps[]");
$(td1).innerHTML("Step " + stepCounter);
stepCounter++;
$(tr).append(td1);
$(tr).append(td2);
$(td2).append(textarea);
$('#giveTheColAnId').append(tr);
});
Remember to include jQuery for this solution
Note that this code might not be perfect, but it should give you a very good start.
You can add these new Boxes with JavaScript/JQuery, if you dont want to reload the page after adding a new box. I would really recommend to have a look at this, instead of trying to do it with PH.
If you really want to do it just with php, you have to have a submit-button in a form
so add something like
<form method="post" action="?">
<input type="submit" name="submit" value="add row">
</form>
Now the PHP-part:
if(isset($_POST['submit'])) {
echo " [your usual tr-td-structure] ";
}
Keep in mind, that after reloading the page, a variable will lose its value.
So if you want to count up, you have to do it with javascript (or determine somehow which Step is the current and insert it in the echo). Of course set the last step in a hidden field inside the form (or append it as a get-param).
Can you use Jquery, Can't you do something like this ?
$(document).ready(function(){
$('#myButton').click(function(){
$(':input').append('<input type="textbox">');
});
});
I have a table where it's rows are generated by a foreach loop.
Each table row has two columns, one with a form and the other with the forms submitted data, which I have called notes. (and updates using a jQuery library without refreshing the browser).
<script>
$(".notes_column").each(function(){
var $myform = $(this).find('.notes_form');
var $mynotes = $(this).find('.notes');
$myform.validate({
submitHandler: function(form) {
$.post('process.php', $myform.serialize(), function(data) {
$mynotes.html(data);
});
}
});
}); // each function
</script>
<table id="myTable" class="tablesorter" border="1" cellpadding="5">
<thead>
<tr>
<th>Notes</th>
<th>Submit Note</th>
</tr>
</thead>
<tbody>
<?php
foreach($myarray as $myvar){
echo '
<tr>
<td>ID LIKE TO PLACE THE content of the div class="notes" HERE, HOW CAN I DO IT?</td>
<td class="notes_column">
<form class="notes_form" action="" method="post">
<input class="q" type="text" name="notes" size="30" placeholder="Place your notes here..." />
<input class="searchsubmit" type="submit" name="submit" value="Submit" />
</form>
<div class="notes"></div>
</td>
</tr>
';
}
?>
</tbody>
</table>
Right now I use a jQuery each function which iterates through each table column, and for each column with the class name ".notes_column" populates a div with the class "notes" with the submitted data.
The question is listed inside the code with capitalized letters, how can I populate the other column with the forms submitted data?
Any ideas?
Ty
I would create a <div> in the first <td> such as
<td>
<div class="put_notes_here">ID LIKE TO PLACE THE content of the div class="notes" HERE, HOW CAN I DO IT?</div>
</td>
Then populate it the same as you did before
$mynotes.closest("tr").find(".put_notes_here").html(data);
Change the selector for your each to $('#myTable tr'), then you can iterate through each in your table.
Then you could either do an each on the tr object to access each td, or make use of jQuery's :first-child and nth-child(2) etc in a subsequent selector
i have textfield inside html table..i want after i type something like "name" in another cell can show all information or description about people who have that name...that information are take from "member_table"...
<tr>
<td>
<td>
<td><input type="text"........></td>
</td>
</td>
</tr>
<tr>
<td> //i want information show here
</td>
<tr>
what's code for it?
Well the answer would be in 3 part.
<tr>
<td> //i want information show here
</td>
<tr>
modify this to
<tr>
<td> <div id="divResult"></div></td>
<tr>
Now you need to write javascript
<script>
// Get the data from the text field
var txtName = document.getElementById("txtName").value;
// Assuming there is a Ajax Function
ajax(txtName);
//Call back function for ajax
function callBack()
{
if(ajaxObj.... )
{
document.getElementById("divResult").innerHTML = ajaxObj.responseText;
}
}
</script>
The third part is getting the required data from Mysql server from PHP.
<?php
$name = $_REQUEST['txtName'];
echo getDataFromMySQL($name);
?>