Somehow this takes me a day and still does not solve, I have other forms that works properly. But this one looks perfect but it simply refresh the page after I submit the form.
<form action="" enctype='multipart/form-data' name= 'myForm' id="myForm" method='post'>
<label>
<span>Product Name</span><input id="product_name" value='<?php echo $product_name;?>' type="text" name="product_name" disabled >
</label>
<label>
<span>Retail Price</span> <input name='retail_price' value="<?php echo $retail_price;?>" type='text' id='retail_price' size='10' disabled >
</label>
<label>
<span>Discount (%)</span> <input name='discount' value="<?php echo $discount;?>" type='text' id='discount' size='10'disabled >
</label>
<label>
<span>Details</span><textarea id="details" class="product_details" placeholder="For any other reasons, please provide details here" name="details"></textarea>
</label>
<label>
<span>Reason to Report</span>
<select name='reason' id='reason' class="required">
<option value = '' > </option>
<option value = 'price10' > Product price is higher than $9.9</option>
<option value = 'shelf' > Product is off the shelf</option>
<option value = 'no_fs' > Product does not have free-shipping</option>
<option value = 'reject' > Seller does not deliver products</option>
<option value = 'price_not_match' > Product Price does not match on Upto9 </option>
<option value = 'category' > Product is in wrong category </option>
<option value = 'other' > Other reasons</option>
</label>
<label>
<input name='submit' type='submit' id='button' class="button" value="Submit Report"/>
</label>
</form>
THen This is my php code :
echo 'before';
if (isset($_POST['submit'])) {
echo 'after';
var_dump($_POST);
$con= mysqli_connect("xxxxx.com"," admin","xxxxx","xxxxx") or die(mysqli_error($con));
$details=mysqli_real_escape_string($con,$_POST['details']);
$reason=mysqli_real_escape_string($con,$_POST['reason']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$sql="INSERT INTO reports (pid,product_name,listing_price,links,discount,retail_price,details,stock_level,reason,report_time,reporter,reporter,ip) VALUES
('$pid','$product_name','$listing_price','$links','$discount','$retail_price','$details','$stock_level','$reason',now(),'$log_username','$ip')";
$query = mysqli_query($con, $sql);
mysqli_insert_id($con);
mysqli_close($con);
header('Location: ' . $_SERVER['HTTP_REFERER']);
echo "successful!";}
The output is: befre
after I added the sql code var_dump($_POST) gives me nothing, so probably the sql code is wrong...
Related
I want a first radio button to be checked by default. When I click get schedule I want to fetch the schedule of that radio selected train number using a WHERE clause.
<?php
while ($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type = radio />" . $res['Train_no'] . "</td>";
?>
<form action="schedule.php" method="POST" class="form-inline">
<input type="submit" value="Get Schedule"/>
</form>
<?php
}
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('traindb', $con);
} else {
die('Could not connect: ' . mysql_error());
}
$selected_val = $_POST['Train_no']; // Storing Selected Value In Variable
echo "You have selected :" . $selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>
You can try this:
if(isset($_POST['Submit'])){
$selected_val = $_POST['Train_no']; // Storing Selected Value In
echo "You have selected :" .$selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE
Train_No='$selected_val'");
}
Here is one example of "select":
<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
<!-- <option value="disable" selected="">Please Select</option> -->
<option value="">Please select</option>
<option value="Under 35">Under 35</option>
<option value=">35 - 44">35 - 44</option>
<option value=">45 - 54">45 - 54</option>
<option value=">55 - 59">55 - 59</option>
<option value=">60 - 64">60 - 64</option>
<option value=">65 - 69">65 - 69</option>
<option value=">70 - 74">70 - 74</option>
<option value=">75 - 79">75 - 79</option>
<option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
$age = $_POST['age'];
if (isset($_POST['age']) && $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>
If you want embed PHP in select options, do it like this:
<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>
For radio button it is like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit">
</form>
</body>
</html>
I have created one form in that I have put one drop down list and group of checkbox.
If user select any item from that drop down list than particular item should updated in database(Ex.first user has select division A from drop down list than the A should enter into database and if after some time same user change division from A to B than in database in place of A data should B when user press update button.
In the group of checkbox if user change (select or deselect) checkbox compare to previous input than respective checkbox value should updated in database.(Ex. If user has checked two checkbox than the particular checkbox value should update in database and after some time same user has checked all three checkbox or only one checkbox and particular change should update in database when user click on update button
Here I have put my code. If anyone know solution than please help.
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db_new", $connection);
$query=mysql_query("SELECT * FROM student_info where id=17");
$row= mysql_fetch_row($query);
$rollno = $row[1];
$name = $row[2];
$division= $row[3];
$std = $row[4];
$gender = $row[5];
$subject=mysql_query("select * from student_info where id=17");
while($fetch=mysql_fetch_object($subject))
{
$r=$fetch->subject;
$sub=explode(",",$r);
}
if(count($_GET)>0){
$rollno=$_GET['rollno'];
$name=$_GET['name'];
$std=$_GET['std'];
$gender=$_GET['gender'];
$update=mysql_query("update student_info set roll_no='$rollno', name='$name',
std='$std', gender='$gender' where id='17'",$connection);
}
mysql_close($connection);
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Form</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery%20validate/jquery.validate.min.js"></script>
<script src="jquery%20validate/additional-methods.min.js"></script>
<script src="form.js"></script>
</head>
<body>
<form id="editform" method="GET" action="task.php">
<div>
<h1>Form</h1>
<label><b>Roll No:</b></label>
<input id="rollno" name="rollno" type="text" value="<?php echo $rollno ?>"> <br><br>
<label><b>Name:</b></label>
<input id="name" name="name" type="text" value="<?php echo $name ?>"> <br><br>
<label><b>Division:</b></label>
<select id="division">
<option value=" " <?php if (!empty($division) && $division == ' ') echo 'selected = "selected"'; ?>>--select--</option>
<option value="A" <?php if (!empty($division) && $division == 'A') echo 'selected = "selected"'; ?>>A</option>
<option value="B" <?php if (!empty($division) && $division == 'B') echo 'selected = "selected"'; ?>>B</option>
<option value="C" <?php if (!empty($division) && $division == 'C') echo 'selected = "selected"'; ?>>C</option>
<option value="D" <?php if (!empty($division) && $division == 'D') echo 'selected = "selected"'; ?>>D</option>
</select><br><br>
<label><b>Std:</b></label>
<input id="std" name="std" type="text" value="<?php echo $std ?>"><br><br>
<label><b>Gender:</b></label>
<input type="radio" name="gender" value="male" <?php if($gender=='male'){ echo "checked=checked";}?>>Male
<input type="radio" name="gender" value="female" <?php if($gender=='female'){ echo "checked=checked";}?>>Female<br><br>
<label><b>Subject:</b></label><br>
<input type="checkbox" name="box[]" class="subject" value="maths" <?php if (in_array("maths", $sub)){echo "checked";}?>>maths<br>
<input type="checkbox" name="box[]" class="subject" value="science" <?php if (in_array("science", $sub)){echo "checked";}?>>science<br>
<input type="checkbox" name="box[]" class="subject" value="english" <?php if (in_array("english", $sub)){echo "checked";}?>>english<br>
<input id="submit" type="submit" value="submit">
<input id="update" type="submit" value="update">
</div>
</form>
</body>
</html>
You can use jquery ajax to update the data on select and on change of dropdown.
$.post('page.php',{parameter:value},function(data){
//operation performed after insertion
});
Basically I have a table with data, and I have a form with boxes and stuff, and what I am trying to do is when the I click on the next or previous button, it will take the next record from the database. I can do the first and the last record, but I cant manage to figure the in between. this what I have.
$sql="SELECT * FROM Emails where ID > 1 ORDER BY UserEmail LIMIT 1";
if ($result=mysqli_query($connection,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
while( $row = mysqli_fetch_array( $result ) )
{
$Email_Field = $row['UserEmail']; //primary key
$Name_Field = $row['UserName'];
$UserTel = $row['UserTel'];
$Drop_Down = $row['Drop_down'];
$MessageType = $row['MessageType'];
$Comments = $row['Comments'];
$SubjectOther = $row['SubjectOther'];
$Check = $row['Request'];
}
<form method="POST" action="Controller_leads.php">
<p><strong>What kind of comment would you like to send?</strong></p>
<input type="radio" <?php if ($MessageType == "Complaint") echo "checked"; ?> name="MessageType" value="Complaint">Complaint
<input type="radio" <?php if ($MessageType == "Problem") echo "checked"; ?> name="MessageType" value="Problem">Problem
<input type="radio" <?php if ($MessageType == "Suggestion") echo "checked"; ?> name="MessageType" value="Suggestion">Suggestion
<br>
<p><strong>What about us do you want to comment on?</strong></p>
<select name="Drop_Down" size="1">
<option value ="Web Site" <?php if ($Drop_Down == "Web Site") echo selected ?>>Web Site</option>
<option value ="Office Hours" <?php if ($Drop_Down == "Office hours") echo selected ?>>Office Hours</option>
<option value ="Pamphlet" <?php if ($Drop_Down == "Pamphlet") echo selected ?>>Pamphlet</option>
</select>
Other: <input type="text" size="26" maxlength="256" name="SubjectOther" value="<?php echo $SubjectOther ?>">
<p><strong>Enter your comments in the space provided below:</strong></p>
<textarea name="Comments" rows="5" cols="42"><?php echo $Comments;?></textarea><br><br>
<strong>Tell us how to get in touch with you:</strong><br><br>
<table>
<tr><td width="45"> Name </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr>
<tr><td width="45"> E-mail </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr>
<tr><td width="45"> Telephone</td> <td><input type="text" size="35" maxlength="256" name="UserTel" value="<?php echo $UserTel ?>"></td></tr>
</table>
<br>
<input type="checkbox" name="Check" <?php if ($Check == "Contact Requested") echo checked; ?> value="Contact Requested">Please contact me as soon as possible regarding this matter
<br><br>
<input type="submit" value="First" name="first">
<input type="submit" value="Next" name="next">
<input type="submit" value="Previous" name="previous">
<input type="submit" value="Last" name="last"> code here
Try adding an offset to the query. On each next click add one to $offset, on each previous, subtract one from offset. Then, include offset in the query like this:
# get the current offset
# initial value
$offset = 1;
# if we have an offset from a previous or next click, use that
if (isset($_POST['offset'])) {
# validate this input to protect against sql injection
if (is_int($_POST['offset'])) {
$offset = $_POST['offset'];
}
# now that we have our current value, see if we need to get the next or previous
if ($_POST['submit']=="Next") {
# next, add one offset
$offset++;
} else if ($_POST['submit']=="Previous") {
# previous, go back one if we are greater than one
if ($offset > 1) {
$offset--;
}
}
}
# query time, give me one result (LIMIT 1), staring at record $offset
$sql = "select SELECT * FROM Emails where UserEmail > 1
ORDER BY UserEmail LIMIT 1, $offset";
In your form add this:
<input type="hidden" name="offset" value="<?php echo $offset; ?>">
On a different note, UserEmail > 1 seems weird, but I don't know your data.
I'm trying to build a manual form on a Wordpress page.
My goal is to create a dropdown menu of all the users with a fixed role (in my case "athlete") and register single users in some sport events.
My code:
<form name="team_captain" method="POST" >
Carnival Name: <input type="text" id="carnival_name" name="carnival_name" />
<input type="submit" name="submit_carnival" value="Create Carnival"/><br />
Event Name: <select name="event_name">
<option value="Ironman Race">Ironman Race</option>
<option value="Board Race">Board Race</option>
<option value="Ski Race">Ski Race</option>
<option value="Surf Race">Surf Race</option>
</select><br />
Athlete Name:
<select name="athlete_name">[insert_php] echo getUsersByRole('athlete','athlete-selector'); [/insert_php]</select><br />
Age Group: <select name="age_group">
<option value="Under 15">Under 15</option>
<option value="Under 17">Under 17</option>
<option value="Under 19">Under 19</option>
<option value="Open">Open</option>
</select><br />
Sex: <input type="radio" name="athlete_sex" value="Male">Male <input type="radio" name="athlete_sex" value="Female">Female<br />
<input type="submit" value="Insert Race"/>
</form>
[insert_php]
if(isset($_POST['submit_carnival'])) {
$carnival_name = $_POST['carnival_name'];
echo "Registrations for ".$carnival_name;
}
elseif(isset($_POST['submit'])) {
$event_name = $_POST["event_name"];
$athlete_sex = $_POST["athlete_sex"];
$age_group = $_POST["age_group"];
$athlete_name = $_POST["athlete_name"];
echo $event_name;
echo $age_group;
echo $athlete_sex;
echo $athtlete_name;
}
[/insert_php]
At this point I can display the name of the carnival, but I can't display the instances of the athletes. And the dropdown list of the athletes doesn't work.
This is the function:
function getUsersByRole($role,$name,$selected = '',$extra = '') {
global $wpdb;
$wp_user_search = new WP_User_Query(array("role"=> $role));
$role_data = $wp_user_search->get_results();
foreach($role_data as $item){
$role_data_ids[] = $item->ID;
}
$ids = implode(',', $role_data_ids);
$r = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where id IN(".$ids .")", ARRAY_A);
$content .='<select name="'.$name.'" id="'.$name.'" '.$extra.'>';
if($selected == ''){
$content .='<option value="" selected="selected">Choose a user</option>';
}else{
$r_user = $wpdb->get_results("SELECT * from ".$wpdb->prefix . "users where ID = ".$selected."", ARRAY_A);
$content .='<option value="'.$selected.'" selected="selected">'.stripslashes($r_user[0]['display_name']).'</option>';
}
for($i=0; $i<count($r); $i++){
$content .='<option value="'.$r[$i]['ID'].'">'.stripslashes($r[$i]['display_name']).'</option>';
}
$content .='</select>';
return $content;
}
Because you're looking for a $_POST["submit"], you'll have to have an input with the name attribute equal to submit. In your case replace this:
<input type="submit" value="Insert Race"/>
with this:
<input type="submit" name="submit" value="Insert Race"/>
Your getUsersByRole() function is already including the "select" tag, so replace this line:
<select name="athlete_name">[insert_php] echo getUsersByRole('athlete','athlete-selector'); [/insert_php]</select><br />
with this, notice that I changed "athlete-selector" to "athlete_name":
[insert_php] echo getUsersByRole('athlete','athlete_name'); [/insert_php]<br />
With those two changes, things should be working, though I haven't studied your getUsersByRole() function intensively, so there could be a bug in there. Let me know in a comment below if this works for you or not.
I have created a form which allows users to edit existing data within a database, I pull information from one page to the next to populate text boxes and select boxes. I have managed to populate the select box with the correct value but when the update statement goes through it deletes or doesn't recognize the pre-existing value. Can anyone help?
if (isset($_POST['submit'])) {
// Process the form
if (empty($errors)) {
$id = $brand["brandId"];
$brandName = mysql_prep($_POST["brandName"]);
$brandCategory = mysql_prep($_POST["brandCategory"]);
$brandKeyword = mysql_prep($_POST["brandKeyword"]);
$addedBy = mysql_prep($_SESSION['username']);
$query = "UPDATE brands SET ";
$query .= "brandName = '{$brandName}', ";
$query .= "brandCategory = '{$brandCategory}', ";
$query .= "brandKeyword = '{$brandKeyword}', ";
$query .= "addedBy = '{$addedBy}', ";
$query .= "dateTime = CURRENT_TIMESTAMP ";
$query .= "WHERE brandId = '{$id}' ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "Brand updated.";
redirect_to("search.php");
} else {
// Failure
$_SESSION["message"] = "Brand update failed.";
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
<?php $layout_context = "user"; ?>
<?php include("../includes/layouts/header.php"); ?>
<?php include("../includes/layouts/navigation.php"); ?>
<div class="section">
<div id="message">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
</div>
<form id="edit_brands" action="edit_brands.php?id=<?php echo urlencode($brand["brandId"]); ?>" method="post">
<h2>Edit Brand Information: <?php echo htmlentities($brand["brandName"]);?></h2>
<p>
<label for="bname">Brand Name:</label>
<input class="textbox" id="bname" type="text" name="brandName" value="<?php echo htmlentities($brand["brandName"]); ?>" autofocus/>
</p>
<p>
<label for="bcategory">Brand Category:</label>
<select class="textbox" id="bcategory" type="text" name="brandCategory">
<option value=""><?php echo htmlentities($brand["brandCategory"]); ?></option>
<option value="Animation">Animation</option>
<option value="Automotive">Automotive</option>
<option value="Beauty and Fashion">Beauty & Fashion</option>
<option value="Comedy">Comedy</option>
<option value="Cooking and Health">Cooking & Health</option>
<option value="DIY">DIY</option>
<option value="Fashion">Fashion</option>
<option value="Film and Entertainment">Film & Entertainment</option>
<option value="Food and Drink">Food & Drink</option>
<option value="Gaming">Gaming</option>
<option value="Lifestyle">Lifestyle</option>
<option value="Music">Music</option>
<option value="News and Politics">News & Politics</option>
<option value="Science&Education">Science & Education</option>
<option value="Sports">Sports</option>
<option value="Technology">Technology</option>
<option value="Television">Television</option>
</select>
</p>
<p>
<label for="bkeyword">Brand Keyword:</label>
<textarea class="FormElement" id="bkeyword" name="brandKeyword" id="brandKeyword" placeholder=""><?php echo htmlentities($brand["brandKeyword"]); ?></textarea>
</p>
<p>
<input type="submit" class="button" name="submit" value="Edit Brand" onclick="return confirm('Do you wish to edit brand?');"/>
</p>
<p>
Cancel
</p>
</form>
</div>
</div>
The best way is to build the select from an array.
For instance:
<?php
$array = array('Animation', 'Automotive', 'Beauty and Fashion ', ...);
echo '<select class="textbox" id="bcategory" type="text" name="brandCategory">';
foreach ($array as $value){
if($value == htmlentities($brand["brandCategory"]){
echo '<option value='.$value.' selected>'.$value.'</option>';
}else{
echo '<option value='.$value.'>'.$value.'</option>';
}
}
echo '</select>;
This way you can check if the value in the array is the same that the one recieved by post and then add the selected attribute to the option tag.