submit php form with array values - javascript

I have form in html like this:
It has multiple values. I want to submit those values with validation. For example, If somebody enter the value of ad length then he must fill all other fields for that particular row. After that I want to submit the form and store all correct values (I mean, there won't be any conflicts in row.) in to the database.
HTML CODE:
<table border="1" style="width:300px">
<tr>
<th>Ad Length</th>
<th>Ad Type</th>
<th>Ad Location</th>
<th>Price</th>
</tr>
<?php for($i=1;$i<=5;$i++){ ?>
<tr>
<td>
<select name="ad_length[]" id="">
</td>
</tr>
And so on for other elements...
This is myPHP code:
$ad_length = $_POST['ad_length'];
$ad_type = $_POST['ad_type'];
$ad_location = $_POST['ad_location'];
$ad_price = $_POST['ad_price'];
foreach($ad_length as $al){
print_r($al." , ");
}
But, I think, I am doing wrong somewhere?
What will be the perfect solution for this?
Thanks.

This should be a place to start.
<?php
if(isset($ad_length)){
if(!isset($ad_type)){
echo 'Please enter type of advert'
}
elseif{
...
}
else{
/* Insert into database */
}
}
?>

Related

Get value from dynamic table mysql php

I create a table with dynamic data from mysql. He seems to :
screen
When I click on the delete button from the second row, and make an alert js with the value of id, he save the id from the first row and not in the row where I cliqued.
My Views :
$('.Btrash').click(function() {
//var id_stream = $('#id_stream').val();
var id_stream = $('#id_stream').attr('value');
var id_compte = $('#id_compte').val();
alert(id_stream);
});
<div class="row" id="Tstream">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Feeds</th>
<th>Data request</th>
<th>Begin Stream</th>
<th>End Stream</th>
<th><em class="fa fa-cog"></em></th>
<th>Status</th>
<th>Access to Data</th>
</tr>
</thead>
<tbody >
<?php foreach ($aAllRequestByIdUser as $aRequestByIdUser) : ?>
<tr>
<td class="vert-align">
<input type="hidden" id="id_compte" value="<?= $iIdCompte ?>">
<input type="text" id="id_stream" value="<?= $aRequestByIdUser['id_Stream'] ?>">
...
My alert always show 74.
Can you help me to find a solution?
Thanks
First let me explain a bit about jquery selectors. This code of yours
var id_stream = $('#id_stream').attr('value');
selects all the items with the id of #id_stream, and returns the first value.
If you want to get the ID of the row clicked, then you should select the row first. But since you haven't shared the rest of your code, i can only assume the rest. There are several ways of doing so, and this is one of them.
1 ) Get the parent of $(this) >
2 ) Then get the first sibling of it
3 ) Finally find input and get val().
Or you can do it by Jquery's closest function. But as i said, first i need to see the rest of your code.

Fetch table row data as per the selected checkbox

I am using Material Design Lite to create a table and I am inserting rows dynamically using php.
The material design lite has provided checkboxes for each row. Now, I want to use the selected row's data on a button click. Does anyone have any idea how to do that?
Here is the code :
<table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--2dp" id="sanctionedTable">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Name</th>
<th class="mdl-data-table__cell--non-numeric">Registration No.</th>
<th class="mdl-data-table__cell--non-numeric">Amount</th>
</tr>
</thead>
<tbody>
<?php
$temp = "";
while($row = mysqli_fetch_assoc($result))
{
$temp="<tr><td>".$row["name"]."</td><td>".$row["regno"]."</td><td>".$row["amount"]."</td></tr>";
echo $temp;
}
?>
</tbody>
</table>
You can use something like this using jquery:
$('tr').click(function(){
alert("Name:"+$(this).children('td:nth-child(1)').html()+",Registration nio:"+$(this).children('td:nth-child(2)').html()+",Amount:"+$(this).children('td:nth-child(3)').html());
});
This fetches the information in each row.

Submit button doesn't work in a form containing table with foreach

I have a form which contains a table. In the table, I used a foreach to fetch multiple data.
This is my code:
<from method="post" action="/provider/provider/checkUserCodeValidation">
<table style="direction: rtl;text-align: right;width: 1500px;font-family: tahoma;" align="center">
<thead>
<tr>
<th style="width:5%; ">row</th>
<th style="width:20%;">Fullname</th>
<th style="width:30%">Title</th>
<th style="width: 5%">Number</th>
<th>Date</th>
<th>ProCode</th>
<th>UseCode</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($Items as $Item)
{
echo '<tr>
<td>'.$i.'</td>
<td>'.$Item->getUser()->getFullName().'</td>
<td>'.$Item->getContractTitle().'</td>
<td>'.$Item->getCount().'</td>
<td>'.$Item->getCreationDate(true,'Date').'</td>
<td>'.$Item->getProviderCode().'</td>
<td><input name=userCode id=userCode></td>
</tr>';
$i++;
}
?>
</tbody>
<input type="submit" name="Submit" id="submit_butt" value="Submit"/>
</table>
</from>
The values in each row are static except the UseCode which is an input tag.
The user inputs a number and by pressing the submit button, the form should post data but it doesn't.
I'd like to know why? Am I even allowed to do this having a foreach in table? If not, what else I should do to achieve this?
The solution in anyway helps! JQuery, Ajax, anything.
You need to give your input element proper name.
Change
<td><input name=userCode id=userCode></td>
To:
<td><input name="userCode[]" id="userCode" value=""/></td>
Please notice the square brackets [] around the name attribute.
As you are posting multiple values, you need to post array not a single value.
In backend PHP, just use,
echo '<pre>';
print_r($_POST['userCode']);
echo '</pre>';
And you will get all posted values within loop.
EDIT:
<form> tag is incorrect.
Change:
<from
To
<form
Also, closing tag.

jquery filter on table works first time but after that hides all rows

I've created a filter to show only rows containing td's with the value selected from a dropdown. The filter works fine first time but second time i run it, all rows dissapear and I cant figure out why.
This is my filter:
$(document).ready(function(){
$('select[name=selectedName]').change(function() {
$('tr').filter(function () {
return $(this).find('td.userName').filter(function () {
return $(this).text().indexOf($('select[name=selectedName]').val()) == -1;
}).length;
}).hide();
});
});
The drop down:
$query = "SELECT user_name FROM users";
$result = mysql_query($query); ?>
<select name="selectedName" id="userSelected">
<option value="" disabled selected>user name</option>
<?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value="<?php echo $line['user_name'];?>">
<?php echo $line['user_name'];?>
</option>
<?php } ?>
</select>
And finally the creation of the table:
<table class="table table-bordered table-hover" ng-controller="tableCtrl">
<thead>
<th>user name</th>
<th>script name</th>
<th>cron format<span class="glyphicon glyphicon-question-sign"></span></th>
<th>schedule last update</th>
<th>next execution time</th>
<th>script exec</th>
</thead>
<tbody ng-repeat="(user_id,script_id) in data">
<tr ng-repeat="(script_id, cron_format) in script_id">
<td class="userName">{{user(user_id)}}</td>
<td class="scriptName">{{script(script_id)}}</td>
<td class="cronFormat"><span contenteditable="true" ng-repeat="l in letters(cron_format) track by $index">{{l}}</span></td>
<td>{{compare(user_id,script_id,cron_format)[0]}}</td> <!--[0] represents scheduler last update-->
<td>{{compare(user_id,script_id,cron_format)[1]}}</td> <!--[1] represents next execution time-->
<td>{{compare(user_id,script_id,cron_format)[2]}}</td> <!--[2] represents script_exec-->
</tr>
</tbody>
</table>
Any idea why is it happening? Thanks for helping...
UPDATE
i added $('tr').show(); and function works except, how can i add value in dropdown to show the all table/cancel the filter?
You take care of hiding rows, but you never show them back. This is why your table sooner or later will have all rows invisible.

On button click, validate that atleast a row must be moved to a custom table with Jquery

I am using MVC3, EF Model first on my project.
I have a view with 4 tables and then I have a CustomPickedTable, whenever a user click on a row inside those 4 tables that row moves to CustomPickedTable Table this is the code for it:
<script type="text/javascript">
$(function () {
$('.questionsForSubjectType tbody tr').click(function () {
var origin = $(this).closest('table').attr('id');
$(this)
.appendTo('#CustomPickedTable tbody')
.click({ origin: origin }, function (evt) {
$(this).appendTo('#' + evt.data.origin);
});
});
});
</script>
What I am looking for is some kind of validation that when a user clicks on the submit button there should be a rule that make sures that atleast one row in each of those 4 tables must be moved to CustomPickedTable if not it should not post the form but give the user an errormessage.
This is one of my 4 tables, these get generated by a foreach loop with razor in MVC
<div class="questionsForSubjectType" id="questionsForSubjectType_1">
<table class="box-style2" id="RandomID_c5b9bc7a-2a51-4fe5-bd3a-75b4b3934ade">
<thead>
<tr>
<th>
Kompetens
</th>
</tr>
</thead>
<tbody>
<tr>
<td data-question-id="16">Har konsulten Förmåga att lära sig nytt?</td>
</tr>
</tbody>
<tbody>
<tr>
<td data-question-id="17">Har konsulten rätt kompetens?</td>
</tr>
</tbody>
</table>
</div>
My custom table:
<table id="CustomPickedTable" class="box-style2">
<thead><tr><th>Choosen Questions</th></tr></thead>
<tbody>
</tbody>
</table>
Thanks in advance!
There might be a better way.
But i would add a data-attribute or some class to each of the TD's you can move, and on submit check for each required value.
Created an example here: http://jsfiddle.net/y35Qf/1/
Basicly i added an attribute called data-row, and each table has its own value, on submit i require each of these values to be in the CustomPickedTable - if not i alert that something is missing - else alert success.
You could easily add so you alert which rows are misssing or any other validation you would want.
Is this what you wanted?

Categories