Page 1 of 1

INSERTING VALUES FROM JAVASCRIPT TO DATABASE USING PHP

Posted: Mon Mar 09, 2009 4:07 am
by sarz
Can you please help me with my codes? The javascript part has no problem, but still,
this code cannot insert any values to database...
Please, please, please try it and help me. I will truly appreciate your help from the bottom of my heart.
p.s. I'm using phpmyadmin for database

Code: Select all

<html>
<head>
 
<script type="text/javascript">
function createForm(number) {
data = "";
inter = "'";
if (number < 51 && number > -1) {
for (i=1; i <= number; i++) {
if (i < 50) spaces="      ";
else spaces="    ";
data = data + "<input type='text' id='" + inter + i + inter + "' name='tester[]' value='<?php echo"$test";?>'><select id='" + inter + i + inter + "' name='tester2[]'><option>'<?php echo"$test2";?>'</option><option value='" + inter + i + inter + "' >Positive</option><option value='" + inter + i + inter + "'>Negative</option> </select> <br>";
}
 
document.getElementById('cust').innerHTML = data;
}
else
{ alert('Maximum of 50 only!');}}
</script>
</head>
 
 

Code: Select all

 
<body> 
[color=#000040]<!--The form; a textbox and a dropdown-->[/color]
 <form name="counter">
 <input type="text" name="number" size="5" />
 <input name="button" type="button" onclick="createForm(document.counter.number.value);" value="Add item(s)" /><br>
 <span id=cust style="position:relative;"></span>
 <input type="submit" name="sub"/>
 </form>
 

Code: Select all

 
 <?php
[color=#000040] //script part[/color]
include("connect.php");
if(isset($_POST["sub"])){
$test=$_POST['tester[]']; $test2=$_POST['tester2[]'];
mysql_query("insert into `testing` (`tester`,`tester2`) values('$test','$test2')");}
 ?>
</body>
</html>
 
 
 
//sql part
CREATE DATABASE `testing` ;
 
CREATE TABLE `testing`.`tester` (
`test` varchar( 12 ) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
 
CREATE TABLE `testing`.`tester2` (
`test2` varchar( 1 ) NOT NULL
);

Re: INSERTING VALUES FROM JAVASCRIPT TO DATABASE USING PHP

Posted: Mon Mar 09, 2009 4:16 am
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]

Re: INSERTING VALUES FROM JAVASCRIPT TO DATABASE USING PHP

Posted: Mon Mar 09, 2009 4:19 am
by susrisha
@astions

was wondering dont you feel a bit annoyed trying to teach each and every newbee about the rules.. just a comment. no offense..


Havent gone much into the details but these are some errors i found out.

Code: Select all

 
[color=#FF0000]<form name="counter">[/color]
 
A form is supposed to have action and method tag if you are using to post or get data from form

Code: Select all

 
 
<form name="counter" action="$PHP_SELF" method="POST">
 
action here refers to the current page itself.

Code: Select all

 
mysql_query("insert into `testing` (`tester`,`tester2`) values('$test','$test2')");
 
its always good to keep debugging statements when you are working it for the first time.

Code: Select all

 
if(!mysql_query("insert into `testing` (`tester`,`tester2`) values('$test','$test2')"))
{
echo "Mysql query error mysql_error()";
}
else
{
echo "query successful";
}
 

Re: INSERTING VALUES FROM JAVASCRIPT TO DATABASE USING PHP

Posted: Mon Mar 09, 2009 4:28 am
by Benjamin
susrisha wrote:@astions

was wondering dont you feel a bit annoyed trying to teach each and every newbee about the rules.. just a comment. no offense..
Well it's important to everyone that members use syntax highlighting. The code is much easier to understand and errors often become obvious. I do get somewhat annoyed when members ask questions without highlighting their code. Instead of reading it and trying to help them I'll move onto the next question.