Add Multiple row of data in databse at once - php

i have few field of data such as product , amount and barcode.The system just show 1 row of data insert form that contain the 3 field.when i completed insert the first row, then second row of textbox will auto generate, i can do it by microsoft access, can i do so for php ?
<?php $i=0; ?>
<form method="post" action="">
<input type="text" name="<?php echo $i; ?>" />
</form>
<?php
if(isset($_POST[$i])){
$i++;
?>
<form method="post" action="">
<input type="text" name="<?php echo $i; ?>" />
</form>
<?php }?>
it work for the first and second textbox, but how can i continue to create more textbox accordingly?

say your file name is 1.php and you will submit the form to 1a.php
1.php
<html>
<head>
<script>
a=0;
function generate()
{
if(document.getElementById(a).value!="")
{
var new_input=document.getElementById(a).cloneNode(true);
//document.getElementById(a).type="hidden";//uncomment this statement to hide the previous input box.
new_input.id=++a;
new_input.title='product_no:'+(a+1);
new_input.value="";
document.getElementById('input_section').appendChild(new_input);
}
else
{
alert("Give some value first.");
document.getElementById(a).focus();
}
}
</script>
</head>
<body>
<form method="post" action="1a.php">
<span id="input_section">
<input type="text" onFocus="this.name='prod[]'; this.id=a; this.title='product_no:'+(a+1);"/>
</span>
<input type="button" value="Insert&Generate" onClick="try{generate()} catch(e){alert(e)}">
<input type="submit" value="INSERT ALL">
</form>
</body>
</html>
1a.php
<html>
<?php
if(count($_POST)==0)
{
?>
<script>
alert('No record to insert.')
location.href="1.php";
</script>
<?php
}
$con=mysqli_connect(...);// connect to database
foreach($_POST['prod'] as $prod_info)
{
$prod_info_arr=explode(",",$prod_info);
$sql="insert into tab1(product, amount, barcode) values('".$prod_info_arr[0]."', ".$prod_info_arr[1].", '".$prod_info_arr[2]."')";
mysqli_query($sql);
}
?>
</html>
This is what you wanted. Check it.
edit: just added comment on the statement document.getElementById(a).type="hidden"; to make the filled textbox not disappear.

If you want to have dynamic input fields, so that second and third input boxes are displayed only when the first input box is filled, use an AJAX code.

This must be what you want. Since you have not mentioned clearly under what condition the second input field should be displayed, i assumed if 'try' is inserted in the first input field, second input field is shown.
<?php
echo "<script>
function showUser(str)
{
if (str=='')
{
document.getElementById('txtHint').innerHTML='';
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('txtHint').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET','get.php?q='+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form method='post' action=''>
First input<input type='text' name='name' onclick='showUser(this.value)' /></br>
<div id='txtHint'></div>
</form>
</body>
</html>";
?>
And in get.php file the following code;
<?php
$q = $_GET['q'];
if($q == 'try'){
echo "Second input<input type='text' />";
}
?>
Hope this will help. And change the get.php code according to the condition you want the second input field to be shown.
This code works. After entering try in the first input field again click inside the text field. And then second input field will be shown.

Related

Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})

form onchange AJAX function for PHP database query

I am quite new to AJAX and would appreciate any help. I have been trying to use AJAX onchange of first drop down menu to call a function for PHP to query database and populate matching results to second drop down menu.
My db connect script works fine, and my PHP query accurately pulls the correct info from the database. (when not using posted variable $cityinput) Problem has been with getting the result from PHP to AJAX, and displayed in the second drop down menu.
<?php
require'connect.php';
$cityinput=$_POST['cityinput'];
$query="SELECT mname FROM masseurs WHERE lounge='$cityinput'";
$result=mysql_query($query);
$num=mysql_numrows($result);
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$mname=mysql_result($result,$i,"mname");
$mname="<option value=''>"mname"</option>";
$i++;}
if (!mysql_query($query))
{die('Error: ' . mysql_error());}
mysql_close();
?>
<html>
<head>
<script>
function getmasseurs()
{if (str=="")
{document.getElementById("masseurinput").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("masseurinput").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","outputpopulate3.php?$mname="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="book" action="test.php" method="post">
<p align="center">
<select name="cityinput" id="cityinput" onchange="getmasseurs()">
<option value="0" selected>City</option>
<option value="1">Brisbane</option>
<option value="2">Sydney</option>
<option value="3">Melbourne</option>
<option value="4">Adelaide</option>
<option value="5">Perth</option>
</select>
</p>
<p align="center">
<select name="masseurinput" size="1" id="masseurinput"><div id="cityinput"></div>
</select>
</p>
<p align="center">
<input type="submit">
</form></p>
</body>
</html>
This appears wrong: xmlhttp.open("GET","outputpopulate3.php?$mname="+str,true);
Which file is outputpopulate3.php? The top one? If not post the code of that file
Why are you using a variable in it ? $mname. You can't just use PHP code inside of javascript.
If you want to that you should use:
xmlhttp.open("GET","outputpopulate3.php?<? echo $mname; ?>="+str,true);
Also, the 'str' variable in this line is never set..
You can set it like this in a previous line:
str = document.getElementById('masseurinput').value;
But probably it would be easier to just use a fixed variable name..

How to pass form input value to php function

I want to write a php page in which there is a html form. I want to send all input (number for example) of my form to a php function (instead of a javascript function; I make this to hide my javascript function code).
How can I send input value to php function?
Is it possible to call the php function through onclick="function(param1, param2)"?
I know that javascript is a client-side language while php is server-side.
If it is possible, how can I write the return of the function in an input field?
I want to remain in my page.
Is it correct - action="#"?
My code is:
<form action="#" method="get">
Inserisci number1:
<input type="text" name="val1" id="val1"></input>
<?php echo "ciaoooo"; ?>
<br></br>
Inserisci number2:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Help me in the implementation of the simple php function and in the passage of values from input to function!
Thanks!
Make your action empty. You don't need to set the onclick attribute, that's only javascript. When you click your submit button, it will reload your page with input from the form. So write your PHP code at the top of the form.
<?php
if( isset($_GET['submit']) )
{
//be sure to validate and clean your variables
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
//then you can use them in a PHP function.
$result = myFunction($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result above the form ?>
<form action="" method="get">
Inserisci number1:
<input type="text" name="val1" id="val1"></input>
<?php echo "ciaoooo"; ?>
<br></br>
Inserisci number2:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" name="submit" value="send"></input>
</form>
You need to look into Ajax; Start here this is the best way to stay on the current page and be able to send inputs to php.
<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<h3>Start typing a name in the input field below:</h3>
<form action="">
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
This gets the users input on the textbox and opens the webpage gethint.php?q=ja from here the php script can do anything with $_GET['q'] and echo back to the page James, Jason....etc
This is pretty basic, just put in the php file you want to use for processing in the element.
For example
<form action="process.php" method="post">
Then in process.php you would get the form values using $_POST['name of the variable]
No, the action should be the name of php file. With on click you may only call JavaScript. And please be aware the hiding your code from the user undermines trust. JS runs on the browser so some trust is needed.
You can write your php file to the action attr of form element.
At the php side you can get the form value by $_POST['element_name'].
you must have read about function call . here i give you example of it.
<?php
funtion pr($n)
{
echo $n;
}
?>
<form action="<?php $f=$_POST['input'];pr($f);?>" method="POST">
<input name=input type=text></input>
</form>

add variable in action in a form

I'm trying to learn HTML and PHP. In an example which i found over the internet i need to set a variable to the submit button. So when the submit button is pressed, this page reloads, with a variable in the address bar,the variable is the one from the drop down menu. like this :
test.php?idneeded=$variable
in which the $variable is selected by the user and then the page reloads to show specific content related to the chosen option.
For example :
test.php?idneeded=40
(40 is "MadTechie" from the drop down form)
the code i found is this :
<?php
if( isset($_GET['ajax']) )
{
//In this if statement
switch($_GET['ID'])
{
case "LBox2":
$Data[1] = array(10=>"-Tom", 20=>"Jimmy");
$Data[2] = array(30=>"Bob", 40=>"-MadTechie");
$Data[3] = array(50=>"-One", 60=>"Two");
break;
//Only added values for -Tom, -MadTechie and -One (10,40,50)
case "LBox3":
$Data[10] = array(100=>"One 00", 200=>"Two 00");
$Data[40] = array(300=>"Three 00");
$Data[50] = array(1000=>"10000");
break;
}
echo "<option value=''></option>";
foreach($Data[$_GET['ajax']] as $K => $V)
{
echo "<option value='$K'>$V</option>\n";
}
mysql_close($dbh);
exit; //we're finished so exit..
}
$Data = array(1=>"One", 2=>"Two", 3=>"Three");
$List1 = "<option value=''></option>";
foreach($Data as $K => $V)
{
$List1 .= "<option value='$K'>$V</option>\n";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Simple Dymanic Drop Down</title>
<script language="javascript">
function ajaxFunction(ID, Param)
{
//link to the PHP file your getting the data from
//var loaderphp = "register.php";
//i have link to this file
var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>";
//we don't need to change anymore of this script
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch(e){
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//the line below reset the third list box incase list 1 is changed
document.getElementById('LBox3').innerHTML = "<option value=''></option>";
//THIS SET THE DAT FROM THE PHP TO THE HTML
document.getElementById(ID).innerHTML = xmlHttp.responseText;
}
}
xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<!-- OK a basic form-->
<form method="post" enctype="multipart/form-data" name="myForm" target="_self">
<table border="0">
<tr>
<td>
<!--
OK here we call the ajaxFuntion LBox2 refers to where the returned date will go
and the this.value will be the value of the select option
-->
<select name="list1" id="LBox1" onchange="ajaxFunction('LBox2', this.value);">
<?php
echo $List1;
?>
</select>
</td>
<td>
<select name="list2" id="LBox2" onchange="ajaxFunction('LBox3', this.value);">
<option value=''></option>
<!-- OK the ID of this list box is LBox2 as refered to above -->
</select>
</td>
<td>
<select name="list3" id="LBox3">
<option value=''></option>
<!-- OK the ID of this list box is LBox3 Same as above -->
</select>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
I haven't started learning JavaScript, and i need this for a project. I'll appreciate it if anyone can help me on this.
Thanks.
i don't understand your question vary well but i'll try to help you in general. in the html be sure that you use the method="get" for the form and in this way the variables are passed to the php in the url. (in other cases POST needed but for now you are ok even with get). all the input's values with the NAME attribute set are passed into the url. ex:
<form action='phpscript.php' method='get' >
<input type='text' name='just_a_test' value='somevalue' />
<input type='submit' value='submit_form' name='submit' />
</form>
the url after submiting will be :
http://mypage.com/phpscript.php?just_a_test=somevalue&submit=submit_form
in the other side the php script that will use the data from the form will be
<?php
if (isset($_GET['submit']) ) {
if (isset($_GET['just_a_test']) )
{
$variable1 = $_GET['just_a_test'];
//do something with variable 1 and output results
//based on the value of this variable.
}
}
?>
you can do the same thing for ass many variables as you want . i hope this was a help to you because i cant undestand your question better than this .
If the form is supposed to be sent during a redirect, you are not using AJAX. In this case the solution is simple:
<form name="myForm" action="test.php" method="GET">
<select name="idneeded">
<option value="40">MadTechie</option>
<option>...</option>
</select>
</form>
Things like these are explained in every HTML tutorial. This is a good starting point: W3C Schools.
You haven't mentioned if the value of the variable is available on client or server?
Variable on Client:
Basically, you will have to handle the onSubmit event of the form. Here you can append the value of the variable to the action.
Variable on Server:
Here you would change the action when you are rendering the HTML.

Insert Cursor Into PHP Drop Down On Page Load

I wonder whether someone may be able to help me please.
Firstly, apologies, I'm sure this is a really simple fix, but I just can't find the answer.
I'm using the script below to create a drop down menu. Once a value has been selected from it, the relevant records are retrieved.
<html>
<head>
<script type="text/javascript">
function ajaxFunction(name)
{
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{// code for IE6, IE5
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("my_div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getfinds.php?dateoftrip="+name,true);
xmlhttp.send();
}
function getquerystring() {
var form = document.forms['frm1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}
</script>
<style type="text/css">
<!--
.style1 {
font-family: Calibri;
font-size: 14px;
}
-->
</style>
</head>
<body onLoad="document.forms.getfinds.name.focus()" >
<form action="getfinds.php" method="get" name="getfinds">
<input name="field" type="hidden" id="field" value="">
<table width="148" border="0">
<tr>
<td width="152"><p class="style1">Select a date from below</p>
<div align="center">
<?php
include("db.php");
$query="SELECT dateoftrip FROM finds GROUP BY dateoftrip ORDER BY dateoftrip DESC";
echo '<select onchange="ajaxFunction(this.value)"><option name="name" value="allrecords">Show All Records</option>';
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<option name='name' value=".$rows['dateoftrip'].">".$rows['dateoftrip']."</option>";
}
echo "</select>";
?>
</div></td>
</tr>
</table>
</form>
<div id="my_div"></div>
</body>
</html>
I'm trying to find a way of inserting the cursor in the drop down menu on page load.
I've tried using the following:<body onLoad="document.forms.getfinds.dropdown.focus()" > but when I run the script I receive the following error: document.forms.getfinds.dropdown is null or not an object.
As I said, I do apologise for the simple question, but I've been looking for a while for the answer.
I just wondered whether someone could perhaps please let me know where I'm going wrong.
Many thanks and regards
Change your select statement to:
echo '<select id="dropdown" name="dropdown" onchange="ajaxFunction(this.value)">
Change your <body> to:
<body onLoad="document.getElementById('dropdown').focus()" >
Take the name="name" attribute out of your <option> tags.
Add the attribute name="dropdown" to your <select> tag.
Then your onLoad will work fine. No need to change it.
<option> tags do not support the name attribute.
Read More:
http://www.w3schools.com/tags/tag_option.asp

Categories