0

I have a page where I have two columns one is Attribute name and Attribute Value. I have a button named Add Attribute Value which adds text boxes to the screen so that an Attribute Name can have multiple Attribute values. I have added the text boxes with a variable i in javascript.

I need to use the "i" variable and also access the text in the textbox using the name "'mytext'+i".

The code written is below:

<html>
<head><title>Add Attributes</title></head>
<body>
<script language="javascript">
var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br>&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&nbsp;&nbsp;<input type='text' name='mytext'+ i><br>"
i++;
}
</script>
<form action= "" method = "POST">
<h1 align = "center"><u>Attribute Management</u></h1>

 &nbsp;&nbsp;&nbsp;&nbsp;
 Attribute Name Attribute Value <br>
 <br><input type="text" name="attname" > 
     <input type="text" name="attvalue">
     <input type="button" value="Add Attribute Value" onClick="changeIt()">
<div id="my_div"></div>

 <b> </b><br>
          <br><br><input type="submit" name = "submit" value = "Add Attribute" > &nbsp; 
          <input type="submit" name = "submit1" value = "User Application" >
</form>
</body>
</html>

<?php

if(isset($_POST['submit'])){
    $att_name = $_POST['attname'];

    $query_string1 = "ALTER TABLE users ADD $att_name varchar(20)";
    $query_string2 = "ALTER TABLE attributes ADD $att_name varchar(20)";
    $part_string = "(";
    for($x = 0;$x<= i;$x++){
        $att_value= $_POST['mytext'.$x];
        if($x = 0){
        $part_string = $part_string.$att_value;
        }
        else{
        $part_string = ",".$part_string.$att_value;
        }
    }
    $part_string= $part_string.")";
    $query_string3 = "INSERT INTO attributes ('$att_name') VALUES '$part_string'";
    $connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");        
    /*$query1 = mysqli_query($connect,$query_string1);
    $query2 = mysqli_query($connect,$query_string2);
    $query3 = mysqli_query($connect,$query_string3);*/
}
if(isset($_POST['submit1'])){
header('Location: RegisteredUsers.php');
}
?>

Get an error of "i" is not defined and "Undefined index: mytext0". I have to collect all the attribute values and fire a query with all the values from the text box together.

Please suggest.

1 Answer 1

1

The error of "Undefined index: mytext0" is come due to no such input field is defined, because you have initialised the i with 1 so, just make it 0.

And also you have a issue in the html, which you are adding, just change your script block with following :

var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br>&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&nbsp;&nbsp;<input type='text' name='mytext"+ i+"'><br>"
i++;
}
<html>
<head><title>Add Attributes</title></head>
<body>

<form action= "" method = "POST">The error of "Undefined index: mytext0" is come due to no such input field is defined, because you have initialised the i with 1 so, just make it 0. 

And also you have a issue in the html, which you are adding, just change your script block with following :

<h1 align = "center"><u>Attribute Management</u></h1>

 &nbsp;&nbsp;&nbsp;&nbsp;
 Attribute Name Attribute Value <br>
 <br><input type="text" name="attname" > 
     <input type="text" name="attvalue">
     <input type="button" value="Add Attribute Value" onClick="changeIt()">
<div id="my_div"></div>

 <b> </b><br>
          <br><br><input type="submit" name = "submit" value = "Add Attribute" > &nbsp; 
          <input type="submit" name = "submit1" value = "User Application" >
</form>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

How can I get the values from the all the textboxes to write a query ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.