<?php include 'connection.php'; ?>
<?php
if (isset($_REQUEST['submit'])) {
$description1 = $_REQUEST['description1'];
$a = $description1;
$description2 = $_REQUEST['description2'];
$b = $description2;
$f = array('$a', '$b');
$g = implode(" ", $f);
echo $g;
$qr = mysql_query("insert into module1 values('','$g')");
}
?>
<html>
<head>
</head>
<body>
<form name="cform" method="post" enctype="multipart/form-data">
<input type="text" name="description1[]" /><br />
<input type="text" name="description2[]" /><br />
<input type="submit" name="submit" />
</form>
</body>
I want to insert the value of description1 & description2 into one feild of a table of database.. How to do that?
Use json_encode()
$f = json_encode(array('$a', '$b'));
Retrieve it using json_decode()
First of all you're sending arrays name="description1[]" so need to use $_REQUEST['description1'][0]
Awful lot of code just to do this
$g = $_REQUEST['description1'][0] . ' ' . $_REQUEST['description2'][0];
but since you asked specifically to use implode
$g = implode(' ', array($_REQUEST['description1'][0], $_REQUEST['description2'][0]));
You can use array_merge to merge your array
$f = array_merge($a, $b);
$g = implode(" ", $f);
echo $g;
The issue here is the way you named the fields. The square brackets make them arrays and so you would need to access $_REQUEST['description1'][0] instead.
Try this code, I cleaned it a little but kept it the same direction you were heading.
<?php include 'connection.php';?>
<?php
if(isset($_REQUEST['submit']))
{
$arrayDescription = array(
$_REQUEST['description1'],
$_REQUEST['description2']
);
$stringDescription = implode(' ', $arrayDescription);
echo $stringDescription
$qr = mysql_query("insert into module1 values('','$stringDescription')");
}
?>
<html>
<head>
</head>
<body>
<form name="cform" method="post" enctype="multipart/form-data">
<input type="text" name="description1" /><br />
<input type="text" name="description2" /><br />
<input type="submit" name="submit" />
</form>
</body>
In HTML why are you using array notation in description1[] and description2[] variables where as these two variables are totally different by name. Use description1 and description2 in names and on PHP side get values through $_POST['description1'] and $_POST['description2']. Rest of your code is correct.
Related
I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}
So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )
This is my first php program. Im trying to compare the result of adding two randomly generated numbers (visually displayed before text field) to the answer i give in the submit. But it just wont work. I pass the result at $value3 and try to compare it to the result of the submit. Any ideas guys?
<head>
<title>
</title>
</head>
<?php
$displayForm=True;
if(isset($_GET['result'])){
$displayForm=False;
}
if($displayForm){
?>
<body>
<form method="GET">
result
<?php
$value1=rand(1,10);
$value2=rand(1,10);
$value3=$value1 + $value2;
echo($value1);
echo("+");
echo($value2);
?>
<input type="text" name="number">
<input type="hidden" name="val3" value="$value3">
<input type="submit" name="result" value="submit">
</form>
<?php
}
if(isset($_GET['result'])){
if(isset($_GET['result'])){
$result = $_GET['result'];
}
if($result==$_GET['val3']){
echo "result right: you re human";
}
else{
echo "lol you are a retarded cat";
}
}
?>
</body>
Try this
<body>
<form method="GET">
result
<?php
$value1=rand(1,10);
$value2=rand(1,10);
$value3=$value1 + $value2;
echo($value1);
echo("+");
echo($value2);
?>
<input type="text" name="number">
<input type="hidden" name="val3" value="<?php echo $value3; ?>">
<input type="submit" name="result" value="submit">
</form>
<?php
}
if(isset($_GET['result'])){
if(isset($_GET['result'])){
$result = $_GET['number'];
}
if($result==$_GET['val3']){
echo "result right: you re human";
}
else{
echo "lol you are a retarded cat";
}
}
?>
</body>
I'm trying to do the push-and-pop stack array in PHP but only the last value is stored. How can I store the other values as well even if I click on the button again & load the same page?
This is what I've done:
<?php
if(!$_GET)
$myStack = array();
else
$myStack[] = "";
?>
<html>
<head> <title>Exercise</title>
</head>
<body>
<form action="test.php" method="get">
Element: <input type="text" name="num" value="0"/><br/>
<input type="submit" name="push" value="push" />
<input type="submit" name="pop" value="pop" />
</form>
<?php
if(isset($_GET["push"])){
array_push($myStack, $_GET["num"]);
foreach($myStack as $val)
echo $val . " ";
}
elseif(isset($_GET["pop"])){
array_pop($myStack);
foreach($myStack as $val)
echo $val . " ";
}
?>
</body>
</html>
Every http request php will execute script with all variables from scratch. You have to use $_SESSION or static variables to save values between requests. To store array in $_SESSION just assign it to key:
$_SESSION["myStack"] = array();
$_SESSION["myStack"][] = 1;
$_SESSION["myStack"][] = 2;
You have a reset at the top off your script. After reload your stack will be empty. Also you have to save your stack into a session var.
Here's the code with using a session to store the array:
<?php
//starts the session
session_start();
$myStack = array();
//gets the array from the session if it exists
if (isset($_SESSION['stack']))
$myStack = $_SESSION['stack'];
?>
<html>
<head> <title>Exercise</title>
</head>
<body>
<form action="test.php" method="get">
Element: <input type="text" name="num" value="0"/><br/>
<input type="submit" name="push" value="push" />
<input type="submit" name="pop" value="pop" />
</form>
<?php
if(isset($_GET["push"])){
array_push($myStack, $_GET["num"]);
foreach($myStack as $val)
echo $val . " ";
}
elseif(isset($_GET["pop"])){
array_pop($myStack);
foreach($myStack as $val)
echo $val . " ";
}
//stores the array in the opened session
$_SESSION['stack'] = $myStack;
?>
</body>
</html>
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
echo "<form method=\"post\" action=mainpage.php?subject_id=".$current_subject."¬e_id=".$current_content.">";
?>
<input type='text' name='list_item' value=''>
<input type='submit' name="new_item" value="New Item">
</form>
The problem is that when one of the GET variables is two words the link doesn't write it that way. So for example if $current_subject="Advanced Chemistry" and $current_content="Valence Electrons" the link will come out as:
<form method="post" action="mainpage.php?subject_id=Advanced" chemistry¬e_id="Valence" electrons>
You need to urlencode() the variables like so:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
$subject = urlencode($current_subject);
$content = urlencode($current_content);
$action = "mainpage.php?subject_id=" . $subject . "¬e_id=" . $content;
?>
<form method="post" action="<?php echo $action; ?>">
<input type="text" name="list_item" value="">
<input type="submit" name="new_item" value="New Item">
</form>
Also, you should get in the habit of validating that data. You probably want to check that they are integers.
Use urlencode() or rawurlencode()
Always quote your attributes and escape your data. Quoted, it would work:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
echo "<form method=\"post\" action=\"mainpage.php?subject_id=" . $current_subject . "¬e_id=" . $current_content . "\">";
?>
<input type="text" name="list_item" value="" />
<input type="submit" name="new_item" value="New Item" />
</form>
But, of course, you should urlencode it first:
<?php
$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];
$url = 'mainpage.php?subject_id=' . urlencode($current_subject) . '¬e_id=' . urlencode($current_content);
?>
<form method="POST" action="<?php echo $url; ?>">
<input type="text" name="list_item" value="" />
<input type="submit" name="new_item" value="New Item" />
</form>
I would probably use http_build_query:
$query = http_build_query(array('subject_id' => $_GET['subject_id'], 'foo' => 'bar'));
<form action="mainpage.php?<?php echo $query; ?>">
I have a suspicion that $query should also be htmlentities'd.
The http_build_query handles the URI encoding, but I'm not sure if it should also be HTML-encoded on top of that (it is an HTML attribute after all).
You should have a look at PHPs urlencode()
$current_subject = urlencode($_GET['subject_id']);
$current_content = urlencode($_GET['note_id']);