I am trying to push the data from this simple html grid into sql but I can't do it. in the beginning, I got the error that there was no index for the var impressions. I fixed that. when I use just the advertiser value I can push the data into mysql while with the second value, I can't succeed. can you explain me what I am doing wrong? Thanks in advance
<?php
include_once 'con_ui.php';
if(isset($_POST['btn-save']))
{
$advertiser = $_POST["advertiser"];
$impressions = (isset($POST["impressions"])?
$_POST["impressions"]:'');
$sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
mysql_query($sql_query);
// sql query for inserting data into database
}
?>
<html>
<head>
</head>
<body>
<form method="post">
<table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1>
<tr>
<th>advertiser</th>
<th>impressions</th>
</tr>
<td>
<select name="advertiser" id="advertiser">
<option value="">Select advertiser</option>
<option value = "Brita ">Brita</option>
<option value = "Sammontana">Sammontana</option>
</select>
</td>
<td name= "impressions" id="impressions" >1000000</td>
<td>
<button type="submit" name="btn-save"><strong>SAVE</strong></button>
</td>
</form>
</body>
</html>
There's a few things wrong here:
Firstly the $POST in
$impressions = (isset($POST["impressions"])?$_POST["impressions"]:'');
Is missing an underscore and should be a $_POST
$impressions = (isset($_POST["impressions"])?$_POST["impressions"]:'');
Secondly the browser won't recognize
<td name= "impressions" id="impressions" >1000000</td>
as a form field. If you want to pass on values that the user can't edit you have a few options. You can use a hidden field like:
<td><input type="hidden" name="impressions" id="impressions" value="1000000">1000000</td>
or you can use a text input and disable user input like
<td><input type="text" name="impressions" id="impressions" value="1000000" disabled></td>
here you have the details of the code that i am using to achieve for adding the raws automatically, have the users entering data and keeping the code editable
Hey thanks for your reply. Problem being is i need to have the user enter the date into the cell and and have and the content should be editable. in addition i'm having a button add row, to insert a new raw but doesn't work if use the form type as input. here you have the full code
<?php
include_once 'con_ui.php';
if(isset($_POST['btn-save']))
{
// variables for input data
$advertiser = $_POST["advertiser"];
$impressions = (isset($_POST["impressions"])?
$_POST["impressions"]:'');
// variables for input data
// sql query for inserting data into database
$sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
mysql_query($sql_query);
// sql query for inserting data into database
}
?>
<html>
<head>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var new_client = document.getElementById("advertiser_row1").innerHTML;
var new_impressions = document.getElementById("impressions_row1").innerHTML;
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
}
</script>
</head>
<body>
<table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1>
<form method="post">
<tr>
<th>advertiser</th>
<th>impressions</th>
</tr>
<td>
<select name="advertiser" id="advertiser_row1">
<option value="">Select advertiser</option>
<option value = "Brita ">Brita</option>
<option value = "Sammontana">Sammontana</option>
</select>
</td>
<td><input type="text" name="impressions" id="impressions_row1" value="1000000"></td>
<td>
<button onclick="myFunction()">Add Row</button>
</td>
<td>
<button type="submit" name="btn-save">Save</button>
</td>
</tr>
</form>
</table>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.