HTML PHP Help Form - php

I'm trying my best to generate a unique link with the following information from ID1 and ID2. I've even tested with echo and paste the following .html to the server and there weren't any respond and also tried it with echo also nothing.
I need help basically , thank you.
<form method="post" action="">
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 1:</h1>
<input type="text" value="" name ="cid" id="cid" />
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 2:</h1>
<input type="text" value="" name ="gid" id="gid"/>
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Copy Your Link Here</h1>
<textarea style="width:500px; height:50px;"></textarea><br /><br /><br />
<input type="submit" value="submit" name="submit" id="submit"/>
</form>
<?php
$cid = $_POST["cid"];
$gid = $_POST["gid"];
if(isset($_POST['submit']))
{
//I want to make a new link $url = "www.something.com/" together with the combination of ID 1 and ID2 .
Example : www.something.com/ID1+ID2 // I want like this if its possible
}
else
{
echo "You've failed";
}
?>
I need help if you don't mind

try this
<?php
$new_link = "";
if(isset($_POST['submit']))
{
if(isset($_POST['cid'],$_POST['gid']))
{
$cid = $_POST['cid'];
$gid = $_POST['gid'];
$new_link = "www.something.com/".$cid.$gid;
// if your input is integer and you want to add them then use this
// $new_link = "www.something.com/".($cid+$gid);
}
}
?>
<form method="post" action="">
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 1:</h1>
<input type="text" value="" name ="cid" id="cid" />
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 2:</h1>
<input type="text" value="" name ="gid" id="gid"/>
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Copy Your Link Here</h1>
<textarea style="width:500px; height:50px;"><?php echo $new_link;?></textarea><br /><br /><br />
<input type="submit" value="submit" name="submit" id="submit"/>
</form>

<form method="post" action="">
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 1:</h1>
<input type="text" value="" name ="cid" id="cid" />
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Enter ID 2:</h1>
<input type="text" value="" name ="gid" id="gid"/>
<h1 style="color:#0CF; font-family:Arial, Helvetica, sans-serif;">Copy Your Link Here</h1>
<textarea style="width:500px; height:50px;"></textarea><br /><br /><br />
<input type="submit" value="submit" name="submit" id="submit"/>
</form>
<?php
if(isset($_POST['submit']))
{
$cid = $_POST["cid"];
$gid = $_POST["gid"];
//I want to make a new link $url = "www.something.com/" together with the combination of ID 1 and ID2 .
$emp = "Example : www.something.com/".$cid.$gid // I want like this if its possible
}
else
{
echo "You've failed";
}
?>

Related

Comment the same again and again

I am trying to make a comments section, where people can comment stuff on my webpage.
All the comments get to a database. Alle that works fine.
The only problem I have is, when i have commented some stuff and reload the webpage it comment the same thing again.
Is there a if statement or something to prevent this?
while ($info = mysql_fetch_array($result)){
echo '<div style="border-style: solid; border-color: #808080; border-width: thin">
<div style="width: 1%"><p style="font-size: 10px; margin: 0px">'.$info['Navn'].'</p></div>
<p>'.$info['Besked'].'</p>
</div>';
}
if (isset($_POST['navn']) && isset($_POST['besked']) && isset($_POST['submit'])) {
$navn2 = $_POST['navn'];
$besked2 = $_POST['besked'];
$data = "INSERT INTO `tester`.`davs` (`Navn`, `Besked`) VALUES ('$navn2', '$besked2');";
$resultalt = mysql_query($data);
if ($resultalt) {
echo "$resultat";
}else{
echo "$resultat";
}
mysql_close();
}
?>
<form action="database.php" method="post" id="commentform">
<label for="comment_author" class="required">Your name</label>
<input type="text" name="navn" id="comment_author" value="" tabindex="1" required="required">
<label for="comment" class="required">Your message</label>
<textarea name="besked" id="comment" rows="10" tabindex="4" required="required"></textarea>
<input type="hidden" name="comment_post_ID" value="1" id="comment_post_ID" />
<input name="submit" type="submit" value="Submit comment" />
</form>
my php code:
http://pastebin.com/bQ7c1MPD
my inputs:
http://pastebin.com/P9uc6Hhz
Use tokens in your HTML form:
<input type="hidden" name="token" value="<?=$_SESSION['token'] = uniqid(); ?>" />
This require this at the top of the PHP script (before any output):
session_start();
For validation:
if(isset($_POST['submit']) && $_POST['token'] == $_SESSION['token']))
Full code: PHP | Form

Required fields in PHP form

I have two php files, form and the results page. Form page accepts user info and the result pages inserts the information into the database and displays it.
form page code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Record Insertion Form</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
echo "<div style='font-size: large; font-family: sans-serif'><center><h1>
<p style='color: white; background-color: black'>Resume Generator</p></h1></center> </div>";
echo "Mandotary fields with <font color='red'>*</font> are required!";
?>
<form id="form1" name="form1" method="post" action="resumeinsert.php">
<fieldset>
<legend>Personal Details</legend>
<input type="hidden" name="id" id="id" />
<br class="clear" />
<label for="name"><font color="red">*</font> Name:</label><input type="text" name="name" id="name" />
<br class="clear" />
<label for="gender"><font color="red">*</font> Gender:</label>
<input type="radio" name="gender" value="Male" id="gender_0" />Male
<input type="radio" name="gender" value="Female" id="gender_1" />Female
<br class="clear" />
<label for="dateofbirth"><font color="red">*</font> Date of birth:</label><input type="text" name="dateofbirth" id="dateofbirth" />
<br class="clear" />
<label for="placeofbirth"><font color="red">*</font> Place of birth :</label><input type="text" name="placeofbirth" id="placeofbirth" />
<br class="clear" />
<label for="address"><font color="red">*</font> Address:</label><textarea name="address" id="address" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset>
<legend><b>Educational Details</b></legend>
<label for="schoolname"><font color="red">*</font> School name:</label><input type="text" name="schoolname" id="schoolname" size=50 />
<br class="clear" />
<label for="qualification"><font color="red">*</font> Qualification(s):</label><textarea name="qualification" id="qualification" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills1"><font color="red">*</font> Skills 1:</label><textarea name="skills1" id="skills1" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills2">Skills 2:</label><textarea name="skills2" id="skills2" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset><legend><b>Other Details</b></legend>
<label for="awards">Awards:</label><textarea name="awards" id="awards" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="Volunteer">Volunteer:</label><textarea name="Volunteer" id="Volunteer" cols="45" rows="5"></textarea>
<br class="clear" />
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" />
</fieldset>
</form>
</body>
</html>
resumeinsert.php page code:
<?php
$mysqli = mysqli_connect("localhost", "hecton", "ccna", "joomladb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$dateofbirth = $_POST['dateofbirth'];
$placeofbirth = $_POST['placeofbirth'];
$address = $_POST['address'];
$schoolname = $_POST['schoolname'];
$qualification = $_POST['qualification'];
$skills1 = $_POST['skills1'];
$skills2 = $_POST['skills2'];
$awards = $_POST['awards'];
$Volunteer = $_POST['Volunteer'];
$query = " INSERT INTO j71mi_resumeinfo ( id, name, gender, dateofbirth, placeofbirth, address, schoolname, qualification, skills1, skills2, awards, Volunteer ) VALUES ( '$id', '$name', '$gender', '$dateofbirth', '$placeofbirth', '$address', '$schoolname', '$qualification', '$skills1', '$skills2', '$awards', '$Volunteer' ) ";
$result = mysqli_query($mysqli, $query);
if ($result === TRUE) {
echo "A record has been successfully inserted into the database!.";
echo "<b><h1><center>My Resume</h1></b></center>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Personal Details</p></div>";
echo "<br>";
echo "<b>Name:</b>".$_POST['name'];
echo "<br>";
echo "<b>Gender:</b>".$_POST['gender'];
echo "<br>";
echo "<b>Date of Birth:</b>".$_POST['dateofbirth'];
echo "<br>";
echo "<b>Place of Birth:</b>".$_POST['placeofbirth'];
echo "<br>";
echo "<b>Home Address:</b>".$_POST['address'];
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Educational Details</p></div>";
echo "<br>";
echo "<b>School Name:</b>".$_POST['schoolname'];
echo "<br>";
echo "<b>Qualification(s):</b>".$_POST['qualification'];
echo "<br>";
echo "<b>Skill 1:</b>".$_POST['skills1'];
echo "<br>";
echo "<b>Skill 2:</b>".$_POST['skills2'];
echo "<br>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Other Details</p></div>";
echo "<br>";
echo "<b>Award(s):</b>".$_POST['awards'];
echo "<br>";
echo "<b>Volunteer Activity:</b>".$_POST['Volunteer'];
echo "<br>";
echo "<br>";
echo "<b><h5><center>End of Resume</h5></b></center>";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
My problem is that user sometimes submit the form without even filling out all the fields and it displays " Name: ", which contains nothing.
How can I make the users to fill out the missing required fields after they click submit button with a red text next to the missing fields ? So that empty fields are not sent to database.
2.Some fields are set to NULL in the database, which means the field is optional. How can I not display that specific field in display page if user enters nothing in the form?
You have to validate the form. Use simple checks to determine if fields are filled correctly. Check for empty, alphanumeric, and what ever else you need. It would probably be best to check in the front end before you send the data over to the server. Here are some examples to get you started:
JS Form validation
http://www.w3schools.com/js/js_form_validation.asp
JS Form validation
http://www.tizag.com/javascriptT/javascriptform.php
PHP Form validation
http://www.w3schools.com/php/php_form_validation.asp
Check out these examples and implement something similar to this. Even if you end up validating the form in JS don't forget to implement the bare minimum checks in PHP.

css for dynamic rows not working

<html>
<head>
<title>truck page</title>
<style type="text/css">
tr {
padding: 4px;
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$db = new mysqli("localhost", "root", "", "db1");
function getdata($form_element_name) {
$var = strip_tags($_GET[$form_element_name]);
return $var;
}
?>
<fieldset>
<legend> add a new truck </legend>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
<label>truck number <input type="text" name="truck_number" /></label>
<label>owner name <input type="text" name="truck_owner_name" /></label>
<label>owner phone <input type="text" name="truck_owner_ph" maxlength="10" /></label>
<input type="submit" name="truck_add" value="add" />
<input type="reset" />
</form>
<?php if(isset($_GET['truck_add'])) {
$truck_number = getdata('truck_number');
$truck_owner_name = getdata('truck_owner_name');
$truck_owner_ph = getdata('truck_owner_ph');
$sql_truck_add = "INSERT INTO truck (truck_number, truck_owner_name, truck_owner_ph)
VALUES ('$truck_number', '$truck_owner_name', '$truck_owner_ph' )";
$result_truck_add = $db->query($sql_truck_add);
header("Location: http://localhost/bkp/truckpage.php");
} ?>
</fieldset>
<table>
<tr>
<th>truck number</th>
<th>owner name</th>
<th>phone</th>
<th>operation</th>
<th></th>
</tr>
<?php
$sql_truck_retrieve = "SELECT * from truck";
$result_truck_retrieve= $db->query($sql_truck_retrieve);
while($row=$result_truck_retrieve->fetch_assoc()) {
$truck_num_current = $row['truck_number']; ?>
<tr id="<?php echo "{$truck_num_current}"; ?>">
<td><?php echo "{$row['truck_number']}"; ?></td>
<td><?php echo "{$row['truck_owner_name']}"; ?></td>
<td><?php echo "{$row['truck_owner_ph']}"; ?></td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_numcur" value="<?php echo "{$row['truck_number']}"; ?>" />
<input type="submit" name="truck_update" value="update" />
<input type="submit" name="truck_delete" value="delete" />
</form>
</td>
<td>
<?php // action to be taken if either update or delete is selected
if(isset($_GET['truck_update'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) { ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_update_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="text" name="truck_num_update" placeholder="truck number" />
<input type="text" name="owner_name_update" placeholder="owner name" />
<input type="text" name="owner_phone_update" placeholder="owner phone" />
<input type="submit" value="save" name="truck_update_yes" />
<input type="submit" value="cancel" name="truck_update_no" />
</form>
<?php }
}
else {
if(isset($_GET['truck_delete'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<span>confirm? </span>
<input type="hidden" name="truck_del_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="submit" name="truck_delete_yes" value="yes" />
<input type="submit" name="truck_delete_no" value="no" />
</form>
<?php }
}
}
//
// action to be taken after delete confirmation
if(isset($_GET['truck_delete_yes'])) {
$truck_del_num = $_GET['truck_del_num'];
$sql_truck_del = "DELETE from truck WHERE truck_number = '{$truck_del_num}'";
$result_truck_del = $db->query($sql_truck_del);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_delete_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
//---------------------------------------------------------------------------
//header("Location: http://localhost/bkp/truckpage.php/#$truck_num_current");
//---------------------------------------------------------------------------
}
}
//
// action to be taken after update confirmation
if(isset($_GET['truck_update_yes'])) {
$truck_update_num = $_GET['truck_update_num'];
$truck_num_update = getdata('truck_num_update');
$owner_name_update = getdata('owner_name_update');
$owner_phone_update = getdata('owner_phone_update');
$sql_truck_update = "UPDATE truck SET truck_number = '$truck_num_update', truck_owner_name = '$owner_name_update', truck_owner_ph = '$owner_phone_update' WHERE truck_number = '{$truck_update_num}' ";
$result_truck_update = $db->query($sql_truck_update);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_update_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
}
}
//
?>
</td>
</tr>
<?php } ?>
</table>
</body>
I am trying to extract values from a db table and add them as table rows. The rows are being shown correctly but when I tried to add css styles for the table rows it did not work. I tried to give a border and padding.I am new to web development and cannot understand why the css part is not working.Can anyone please tell how to make the css part work in this php code?
The problem lies in that you cannot very effectively style tr elements. You will need to target the parent table or the child td elements to get the affect you're looking for.
Check out this JSFiddle example: http://jsfiddle.net/v6zNt/
CSS:
table {
border: 1px solid #000;
}
/* notice how these styles does not apply anywhere */
tr {
border: 1px solid green;
padding: 4px;
}
td {
border: 1px solid red;
padding: 4px;
}
HTML:
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
You can style tables, it's just that they have their own set of rules that you need to be mindful of. Here's a great introduction: http://css-tricks.com/complete-guide-table-element/

PHP $_POST returns null values

This is my html code:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
This is the subscribe.php file:
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
print_r($_POST);
if (isset($_POST["subName"]) && isset($_POST["subEmail"])){
$subUser = $_POST["subName"];
$subEmail = $_POST["subEmail"];
echo "$subUser"."<br />"."$subEmail";
}
?>
I have really tried a lot of things out there on the Internet and nothing seems to work for me. Any ideas?
Also looks like the get method works for this...
Could by related to your nginx configuration.
Try:
$post = file_get_contents("php://input");
This should work:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
if($_POST) {
$subName = mysqli_real_escape_string($con, strip_tags($_POST['subName']));
$subEmail = mysqli_real_escape_string($con, strip_tags($_POST['subEmail']));
if(isset($subName) && !empty($subName) && isset($subEmail) && !empty($subMail)) {
echo 'Name: '.$subName.'<br> Email: '.$subEmail;
}
}
?>

value $_POST from others page can't pass throught isset form

I have a problem on pass value $result from different page into another page.
when I POST the value $result into this page. It works fine and I echo it out.
but if i try to pass this $result into isset() form and post it back to self.
The value will become Undefined index.
PAGE1
<?PHP
$connect=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('survey', $connect);
$SQL="SELECT * FROM question";
$data=mysql_query($SQL);
$num=mysql_num_rows($data);
$start=0;
?>
<style type="text/css">
#survey{
width:100%; height:50px;
background:#e6e6e6; border-bottom:solid 1px white;
}
#survey form{
position:relative;
left:320px; top:15px;
float:left;
}
#survey form a{
text-decoration:none;
color:black;
}
.submit{
background-color: transparent;
border: none;
cursor: pointer;
}
</style>
<html>
<div id="container">
<?PHP
if($num<3){
echo "
<div id='create'><a href='question.php'>+ Create a Survey</a></div>
";
}
?>
<?PHP
while($start<$num){
$result=mysql_result($data, $start, 'ID');
$list_num=$start+1;
$ID=$result['ID'];
$list="
<div id='survey'>
<span style='font-size:24px; color:gray; position:relative; float:left; left:10px; top:13px;'>
SURVEY 0$list_num
</span>
<form action='survey.php' method='POST'>
<input class='submit' type='submit' name='submit1' value='View' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='update.php' method='POST'>
<input class='submit' type='submit' name='submit2' value='Edit' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='delete.php' method='POST'>
<input class='submit' type='submit' name='submit3' value='&#10005 Delete' />
<input type='hidden' name='result' value='$ID'/>
</form>
</div>
";
echo $list;
$start++;
}
?>
</div>
</html>
PAGE2
<?PHP
$result=$_POST['result'];
$error="";
if(isset($_POST['submit'])){
$question=$_POST['question'];
$answer_a=$_POST['a'];
$answer_b=$_POST['b'];
$answer_c=$_POST['c'];
if($question && $answer_a && $answer_b && $answer_c){
$connect=mysql_connect("localhost", "root", "");
$db=mysql_select_db("survey", $connect) or die("");
$SQL="UPDATE question SET question='$question', answer_a='$answer_a', answer_b='$answer_b', answer_c='$answer_c'
WHERE ID='$result'";
mysql_query($SQL);
$SQL="UPDATE answer SET question='$question', answer_a='0', answer_b='0', answer_c='0'
WHERE ID='$result' ";
mysql_query($SQL);
header("location:create.php");
}
else{$error="<div style='color:white; background:gray; width:200px; height:20px; text-align:center; line-height:20px;'>"
. "Please fill all the field." . "</div>";}
}
?>
<style>
body{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
</style>
<html>
<form action="update.php" method="POST">
Question<br /><input type="text" name="question" placeholder=" Enter your question here" maxlength="50" /><br/><br/>Answer<br/>
a<input type="text" name="a" placeholder=" Answer a" maxlength="50"/>
b<input type="text" name="b" placeholder=" Answer b" maxlength="50"/>
c<input type="text" name="c" placeholder=" Answer c" maxlength="50"/>
<input type="submit" name="submit" value="SAVE"/>
</form>
<?PHP echo $error; ?>
</html>
Hi All thank you very much for help
I have found the solution
I think the problem is when I pass the value to 2nd form, I will need to resend the value again. Here is what I did.
1st one is from previous page, 2nd one is form post to itself
if(isset($_POST['result'])){
$result=$_POST['result'];
echo $result;
}
if(isset($_POST['data'])){
$ID=$_POST['data'];
echo $ID;
}
add another hidden in the form and resend the value
<input type="hidden" name="data" value="<?PHP echo $result;?>"/>
change the mysql value
WHERE... ID='$ID'";
Try editing on page 2 this
<html>
<form action="update.php" method="POST">
To this
<html>
<form action="" method="POST">

Categories