I have been doing a tutorial on posting to a database with Ajax and now I have it working with 1 form field, but I would like to learn how to get it to work with 2 form fields.
The form field I have working is named content_txt and it is inserted into add_delete_record(content). The second form is named balance_txt and would also be inserted in the same table as content_txt was.
This is the block of code that I'm trying to figure out how to update with the extra form field. I have added notes in it to explain what I am using each thing for and what I want to do:
if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0)
{ //checks $_POST["content_txt"] is not empty, but I am not sure how to add the other field into this
$contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//using this to strip stuff from the post, but I am not sure how I should added the other form field to this, should I create another variable line or can I add it to this.
// Insert sanitize string in record and I think I have it correct, except if I need to add a variable following content
if(mysql_query("INSERT INTO add_delete_record(`content`,`balance`) VALUES('".$contentToSave."')"))
{
//This returns the results back to the page, do I need to have a duplicate section for the additional record or will this work like a loop.
$my_id = mysql_insert_id(); //Get ID of last inserted row from MySQL
echo '<li id="item_'.$my_id.'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo $contentToSave.'</li>';
mysql_close($connecDB); //close db connection
if you use two form just named same field name "content_txt" and post it .
or when you ajax data change it name on ajax field .
Related
I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.
Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.
Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';
What I am trying to do is I have a form which inserts data into a table. But now I want to add a preview button to the form to show a preview of the data to be inserted and uploader can check the formatting he has done before inserting data.
Now I want to know how to submit form to two different pages and use the form data.
I am confused as Using action attribute of form will make both buttons to POST to same page.
I have also tried code below, but it will also fail if the data is too much and as data3 is paragraph and can have long essay data
if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['preview']) ){
$data1 = trim($_POST['data1']);
$data2 = trim($_POST['data2']);
$data3 = trim($_POST['data3']);
header("location: preview.php?preview=true&data1=$data1&data2=$data2&data3=$data3");
exit();
}
Use a session, and store the post data in an array, or output the variables in a hidden form on preview page.
Example 1:
session_start();
$_SESSION['form_data'] = [
'data1' => $_POST['data1']
...
];
Example 2:
echo '<input type="hidden" name="data1" value="', htmlspecialchars($_POST['data1']), '" />';
I have this while loop, which echos out a form onto a page.
So this page is essentially filled with all rows from the DB, (sort of like a news feed with posts)
I have a button at the end of the form, which lets users reply to this post.
How do I set the variables, to take the values from whichever post they click apply on, and take them to a new PHP document which is the application page where users can fill in another form.
while($value= mysqli_fetch_assoc($result)){
echo '<div class="form">';
echo"<div class='field-warp'>".$value['brand_uid']."</div>";
echo"<div class='field-warp'>".$value['value1']."</div>";
echo"<div class='description'>".$value['value2']."</div><br>";
echo"<div class='field-warp'>".$value['value3']."</div><br>";
echo"<button class='button button-block' name='apply' />Apply to
collaborate</button>";
echo '</div>';
}
Your feed database table probably has an id column or some other unique identifier.
So have the button be a link or turn it to a link and style it as a button. Currently your button could look like this:
echo '<button class="button button-block" name="apply" onclick="location.href=\'http://your_domain.com/feed_apply.php?id='.$row["feed_id"].'\';" />Apply to collaborate</button>';
Then in your feed_apply.php you can fetch that id like so:
$feed_id = $_GET['id'];
$feed_data = [];
// query your database to set $feed_data
Now you can pre-populate or use data elsewhere in your application form.
Why not replace the button with an a href link with a GET parameter with the post ID, then on the processing page you retrieve the post ID and its information with your new form?
I am trying to insert the data of text area of tabs in MYSQL using PHP. I am able to insert the data of only predefined tab but I am not able to insert the data of newly created tabs when user clicks on ADD TAB button and create his own tab.
Any ideas how it can be done?
here is my HTML output: http://jsfiddle.net/HMv9S/1/
I have tried this MySQL code for inserting the data and successfully can insert the data of predefined tab:
<?php
if ($_POST['submit'])
{
$con=mysqli_connect("localhost","root","","my_db");
$texts=array($_POST['txt']);
foreach($texts as $value)
{
$allTextArea = implode(",", $value);
$text = mysql_real_escape_string($allTextArea);
$query= "INSERT INTO Sections(data) VALUES('$text')";
mysqli_query($con,$query);
}
}
?>
You should name the textarea as an array like txt[] instead of txt. Now on form submit you will get all the textarea values.
if ($_POST['submit']){
$allTextArea = $_POST['txt'];
print_r($allTextArea);
}
Input elements attribute name must be unique (semantically also id). Then iterate through possible keys received in $_POST.
In single form you are creating multiple input elements (in your case <textarea>) with same name attribute. in $_POST, values are sent in format 'key' => value with input element attribute name as key. All your textareas has 'txt' as name.
I have two pages, product.php and viewcart.php. When a user clicks on a button on product.php, it adds the product to the back-end (along with it’s attributes) and goes to viewcart.php. The data gets submitted to viewcart.php through the code below. There are some php variables that need to get passed in addition to a value from a textbox on product.php (the variable is “val” below).
<script language="javascript">
function test()
{
var val=document.getElementById("textarea").value;
var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;
document.getElementById("a_link").href=hrf;
}
</script>
<i class="icon-shopping-cart icon-white"></i> Add to Cart
On viewcart.php, the product gets added to the back-end and then gets displayed via the code below:
#$link = $_GET['link'];
$price = $_GET['price'];
$title = $_GET['title'];
$retailer = $_GET['retailer'];
$options = $_GET['options'];
$session = session_id();
$_SESSION['sess_var'] = $session;
//connect to database code here
mysql_query("INSERT INTO sessionid (sessionid, link, retailer, price, title, qt, options) VALUES('$session' , '$link', '$retailer', '$price', '$title', 1, '$options') ");
The problem I am having is when the user refreshes viewcart.php; the product gets added again because of the code above. How do I ensure the product gets added to the database ONLY if the user clicks on the submit button on product.php (and not by refreshing viewcart.php or clicking the “back” button to get to viewcart.php)?
Technically if you refresh the page with the same POST data, as far as I know, you will have the same referrer, so you shouldn't try to check whether the referrer is product.php .
What you should do is use an update/insert MySQL query. First make sure that you also insert the primary key in your query (so that when the page is refreshed, it will be the same key), then use something similar to:
INSERT INTO sessionid (id,a,b,c) VALUES (0,1,2,3)
ON DUPLICATE KEY UPDATE id=id;
Lastly, don't forget to sanitize your input.
// where submit is the name of the forms submit input/button
if ($_GET["submit"]) {
// add to cart
} else {
// products should have already been added
}
You have to ckeck if the row has already been added.
So use a select and if "fetch()" return something different than "false" insert your datas.
This problem is often solved using the POST/Redirect/GET pattern. Generally if you have a file called viewcart.php then it should just display the cart and not add a product as well.