html submit button not working - php

As far as I can tell, clicking on submit button does not take me to the next page.
Here is add_product.php which is included in ./admin/index.php and this is the form I have a problem with:
<div class='content'>
<form type='./inc/add_sql.php' method='POST'>
Name: <br/>
<input type='text' name='name' /><br/><br/>
Discription: <br/>
<textarea rows='10' cols='32' name='discription'>
</textarea><br/><br/>
Price (£): <br/>
<input type='text' name='price' /><br/><br/>
Use Stock Values?<br/>
<input type='radio' name='use_stock' value='1' /> Yes &nbsp
<input type='radio' name='use_stock' value='0' /> No <br/><br/>
Image Text: <br/>
<input type='text' name='img_alt' /><br/><br/>
Thumbnail Text: <br/>
<input type='text' name='thumb_alt' /><br/><br/>
Stock Level (Optional)<br/>
<input type='int' name='stock' /><br/><br/>
<input type="submit" value="Submit">
</form>
</div>
and add_sql.php (from above)
<?php
include './sql_connect.php';
$name = mysql_real_escape_string($_POST['name']);
$discription = mysql_real_escape_string($_POST['dicription']);
$price = mysql_real_escape_string($_POST['price']);
$use_stock = mysql_real_escape_string($_POST['use_stock']);
$img_alt = mysql_real_escape_string($_POST['img_alt']);
$thumb_alt = mysql_real_escape_string($_POST['thumb_alt']);
$stock = mysql_real_escape_string($_POST['stock']);
$mysql = mysql_query("INSERT INTO `products`(`name`, `dicription`, `price`, `use_stock`, `img_alt`, `thumb_alt`, `stock`) VALUES ({$name},{$discription},{$price},{$use_stock},{$img_alt},{$thumb_alt},{$stock})");
if (!mysql_query($mysql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added <a href='./?add_products'>continue</a>";
mysql_close($con);
?>
can anyone see what is wrong here?

<form type='./inc/add_sql.php'
should be:
<form action='./inc/add_sql.php'

Related

How to get the data permanently recieved by POST method in a file?

Good day experts!!!
In the code below, the copy.php file will create another php base on input values and will send input values on the newly created php file. On the same page, at first the codes works well but if I open the newly created php file directly, it will give an error PHP Warning: Undefined array key "subject/etc.......). How do I put the values permanently on the post section of the newly created file replacing the post code?
Here's my code below:
copy.php
<?php
$file = 'data.php';
$newfile = $_POST["newFileName"].'.php';
if (!copy($file, $newfile)) {
echo "failed to copy";
}else {
echo "<center><form action='$newfile' method='post'>
Name of the exam: <br><input type='text' name='subject' required><br><br>
Instruction: <br><input type='text' name='instruction' required><br><br>
Questions:<br>
<input type='text' name='new_1'><br>
<input type='text' name='new_2'><br>
<input type='text' name='new_3'><br>
<input type='text' name='new_4'><br>
<input type='text' name='new_5'><br><br>
<input type='submit' value='Create Quiz'>
</form></center>";
}
?>
newfile.php
<?php
$subject=$_POST['subject'];
$instruction=$_POST['instruction'];
$q1=$_POST['new_1'];
$q2=$_POST['new_2'];
$q3=$_POST['new_3'];
$q4=$_POST['new_4'];
$q5=$_POST['new_5'];
echo
"<form action='' method='post' onsubmit='alert('SUBMITTED SUCCESFULLY!')'>
Name: <input type='text' name='NAME' required><br><br>
$subject <br><br>
$instruction <br><br>
<input type='text' name='1' required> $q1 <br>
<input type='text' name='2' required> $q2 <br>
<input type='text' name='3' required> $q3 <br>
<input type='text' name='4' required> $q4 <br>
<input type='text' name='5' required> $q5 <br><br>
<input type='submit' value='submit' onclick='return Confirm();'>
</form>";
?>
You can't open newfile.php directely and have the answers.
The copy file call newfile with the form. newfile expect to receive POST datas. If you open directly newfile, you don't have POST data because no datas where sent.
What you can do :
In copy PHP start a session (session_start()) and give a name to the submit button : <input type='submit' value='Create Quiz' name='subForm'>
In newfile.php you can have something like that :
<?php
session_start();
if(isset($_POST['subForm'])){
$_SESSION['copyForm'] = serialize($_POST);
}
$myForm = unserialize($_SESSION['copyForm']);
$subject=$myForm['subject'];
$instruction=$myForm['instruction'];
$q1=$myForm['new_1'];
$q2=$myForm['new_2'];
$q3=$myForm['new_3'];
$q4=$myForm['new_4'];
$q5=$myForm['new_5'];
echo
"<form action='' method='post' onsubmit='alert('SUBMITTED SUCCESFULLY!')'>
Name: <input type='text' name='NAME' required><br><br>
$subject <br><br>
$instruction <br><br>
<input type='text' name='1' required> $q1 <br>
<input type='text' name='2' required> $q2 <br>
<input type='text' name='3' required> $q3 <br>
<input type='text' name='4' required> $q4 <br>
<input type='text' name='5' required> $q5 <br><br>
<input type='submit' value='submit' onclick='return Confirm();'>
</form>";
?>

Why is my PHP string variable breaking where there is a space? [duplicate]

This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 1 year ago.
My PHP variable 'contractorcompany' usually has spaces in its value but when I try to use the value in a form it breaks at the space.
PHP
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value=" .$contractorid. ">
First name: <input type='text' name='firstname' value=" .$contractorfirstname. ">
<br/>
Last name: <input type='text' name='lastname' value=" .$contractorlastname. ">
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value=" .$contractoremail. ">
<br/>
Phone: <input type='text' name='phone' value=" .$contractorphone. ">
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
When this value is for example 'Grant Grouting', it breaks as per line 6 below.
<form method="post">
<input type="hidden" name="contractorid" value="3">
<input type="hidden" name="contractorfirstname" value="Tom">
<input type="hidden" name="contractorlastname" value="Grant">
<input type="hidden" name="contractoremail" value="tomgrant#grouting.com">
<input type="hidden" name="contractorcompany" value="Grant" grouting>
<input type="hidden" name="contractorphone" value="">
<input name="editcontractor" type="submit" value="Edit this contractor">
</form>
I've tried every combination of quotation marks around the variable but am still not getting the full string passed through the form.
You just missed the quotes for the values:
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value='" .$contractorid. "'>
First name: <input type='text' name='firstname' value='" .$contractorfirstname. "'>
<br/>
Last name: <input type='text' name='lastname' value='" .$contractorlastname. "'>
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value='" .$contractoremail. "'>
<br/>
Phone: <input type='text' name='phone' value='" .$contractorphone. "'>
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
Try to sanitize the output with addslashes for example.

Hot to append form input to appear as comment section user ID?

I am working on a comment section for a project.
I have the following php code:
<?php
echo "<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uID' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea><br>
<button class='submit-button' type='submit' name='commentSubmit'>Comment</button>
</form>";
getComments($conn);
?>
The setComments() and getComments() are here:
function setComments($conn) {
if(isset($_POST['commentSubmit'])){
$uID = $_POST['uID'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO commentSection (uID, date, message)
VALUES ('$uID', '$date', '$message')";
$result = $conn->query($sql);
}
}
function getComments($conn){
$sql = "SELECT * FROM commentSection";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<div class='comment-box'><p>";
echo $row['uID']."<br>";
echo $row['date']."<br>";
echo nl2br($row['message']);
echo "</p>
<form class='delete-form' method='POST' action='deleteComments.php'>
<input type='hidden' name='commentID' value='".$row['commentID']."'>
<button type='submit' name='commentDelete'>Delete</button>
</form>
<form class='edit-form' method='POST' action='editComment.php'>
<input type='hidden' name='commentID' value='".$row['commentID']."'>
<input type='hidden' name='uID' value='".$row['uID']."'>
<input type='hidden' name='date' value='".$row['date']."'>
<input type='hidden' name='message' value='".$row['message']."'>
<button>Edit</button>
</form>
</div>";
}
}
Now, I have created the following form element using bootstrap:
<form>
<div class="row">
<div class="col">
<input type="text" class="form-control" placeholder="First name">
</div>
</div>
</form>
How can I make it so that this form input, or the first name, value shows up here:
<input type='hidden' name='uID' value='Anonymous'>
instead of the default 'Anonymous' value?

Post values to form fields?

I have a form for adding products and their detail to a mysql db.
I have another form that shows the products where I can remove the product from the db, I can also hide or show the product in the website.
I need to be able to edite the product details too. Is there any way that when I choose to edit a products I can get it details to appear in the first form again? The form is for adding details so can it be used to edit them too?
This is my code for first product adding form.
<form class='productsaddform' action='productsadd.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<?php
include '../inc/categorydropdown.php';?>
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type=text name="aname" /><br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<p><label for="cat">Category</label>
<select name="cat" id="cat">
<?php echo $op;?>
</select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="subcat"> </select></p>
<br />
<br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/add_products.php'; ?>
</form>
And this is the form to display the products
<form class='productsremoveform' action='productsadd.php' method='post'>
<?php include '../inc/categorydropdown.php'; ?>
<?php include '../inc/remove_products.php'; ?>
<span class='formheading'>Remove/Hide/Show Products</span><br /><br />
<p><label for="cat">Category</label>
<select name="cat" id="removecat"> <?php echo $op;?> </select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="removesubcat"> </select>
<input type='submit' name='show' value='Show' /> </p>
<?php
include '../inc/connect.php';
if(isset($_POST['show'])){
$subcat = intval($_POST['subcat']);
$q = "SELECT * FROM products WHERE subcat = $subcat";
$result = $link->query($q);
if($result){
while($row=mysqli_fetch_array($result)){
echo "<div class='removeproducts'>";
echo "<input type='checkbox' name='remove[{$row['id']}]' value='Remove'>Remove";
echo "<br />";
echo "<input type='checkbox' name='edit[{$row['id']}]' value='Edit'>Edit";
echo "<br />";
if ($row['status'] == 1){
echo"<input type='checkbox' name='hide[{$row['id']}]' value='Hide'>Hide";
echo "<br />";
}
if ($row['status'] == 2){
echo"<input type='checkbox' name='show[{$row['id']}]' value='Show'>Show";
echo "<br />";
}
echo "<br />",
"<span class='productthumbcode'>",
"{$row['code']}",
"</span>",
"<div id='thumb'>",
"<img class='resizedimage' src='{$row['image']}' alt='{$row['name']}' />",
"</div>",
"</div>";
}
echo "<br style='clear:both' />";
}
else{
echo mysqli_error();
}
}
?>
<input type='submit' id='submit' name='submit' value='Submit' />
</form>
EDIT;
This is the form on my new edit page.How do i get the relevent details from the db into the input values?
<?php if (isset($_GET['pid'])) { ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
This is what i have now, but the form doesnt display now
<?php if (isset($_GET['pid'])) {
$q = mysqli_query($link, "SELECT * FROM products WHERE id = '".$_GET['pid']."'") or
die (mysqli_error());
$row = mysqli_fetch_array($q); ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" value=<?php"$row['name'];"?> />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
EDIT:
Now I want to be able to search products by code in my first form then have that products details displayed in the form in my edit_product page as it is when i edit a product as we sorted. Any idea how to go about this?
This wouldn't be the necessarily the cleanest way to do it, but you could get the information from the db (as you would normally), put it into an array and for each input have something like this:
<input type="text" name="aname" value="<?php echo (isset($array['name'])) ? $array['name'] : ''; ?>" />
Basically, it's an inline if statement that checks to see if the variable is set and if it is, it sets the value of the input to that.
Hope this helps!
If you have any questions on this, let me know :)
EDIT
To answer your question from the comments you could have a link for each product which then takes you to an edit product page e.g.
Link:
AND then just have a page similar to you first page that checks to see if the get variable is set.

Multiple post data to Mysql DB

I have problem with: I want to create admin for add questions to quiz system. Structure is:
<label>Question 1</label>
<input type='text' name='question' value=''/>
<label>Possible reply</label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>**Correct reply</label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
And I need multiple questions post to Mysql db tables: question, 1,2,3,4,5,6, correct.
I was create this:
<?php
if(isset($_POST['submit']))
{
$question $_POST['question '];
$a = $_POST['1'];
$b = $_POST['2'];
$c = $_POST['3'];
$d = $_POST['4'];
$e = $_POST['5'];
$f = $_POST['6'];
correct = $_POST['correct '];
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$result', '$a', '$b', '$c', '$d', '$e', '$f', '$correct' )");
}
else
{
?>
<label>Question 1 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
<?
}
But this send only 1 question to DB.
If you can't use
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
Write in HTML:
<input type='text' name='question[]' value=''/>
<input type='text' name='reply1[]' />
<input type='text' name='reply2[]' />
<input type='text' name='reply3[]' />
<input type='text' name='reply4[]' />
<input type='text' name='reply5[]' />
<input type='text' name='reply6[]' />
<input type='text' name='correct[]' />
In PHP:
<?php
if (isset($_POST['submit'])){
$questions=$_POST['question'];
$reply1=$_POST['reply1'];
$reply2=$_POST['reply2'];
$reply3=$_POST['reply3'];
$reply4=$_POST['reply4'];
$reply5=$_POST['reply5'];
$reply6=$_POST['reply6'];
$correct=$_POST['correct'];
foreach($questions as $key=>$value){
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$value', '".$reply1[$key]."', '".$reply2[$key]."', '".$reply3[$key]."', '".$reply4[$key]."', '".$reply5[$key]."', '".$reply5[$key]."', '".$correct[$key]."' )");
}
}
It would be nice if you can state your database structure and expected result, because it may not work the way you think and we'll never know until we see it.
If you want 1 answer per row, you need to use a multiple row insertion - How to insert multiple rows in single insert statement?.
try that:
<input type='text' name='question1' value=''/>
<input type='text' name='question2' value=''/>
instead of
<input type='text' name='question1' value=''/>
<input type='text' name='question1' value=''/>

Categories