I need to build a page where the user can enter the name of the categories of products of an webstore. I created a page with dynamic input fields so the user will be able to add all the categories (which i don't know the ammount). I have to get the name of those categories and put into a database. I got a script online and made a few changes but i dont know how to proceed further on. I need help with the php script, how to get the array and insert into the database.
This is my form script:
<form method="post" action="setup3.php">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
<input type="text" name="mytext[]">
</div>
</div>
<div>
<input type="submit" value="Next" />
</div>
</form>
I think this would point you in the right direction:
<?php
if (! empty($_POST['mytext'])) {
foreach ($_POST['mytext'] as $entry) {
// perform your insert here.
}
}
I would strongly recommend using an ORM like Doctrine (http://www.doctrine-project.org/) since it already tackles a lot of security issues for you.
If you wish to stay pretty close to low-level database interactions, writing plain SQL queries, I'd recommend using PDO (http://php.net/manual/en/book.pdo.php)
Related
Below is my form- which value i like to update into database.
<form action="#" class="form">
<div class="form__slider">
<div class="form__slider-rating" id="slider__rating"></div>
<div class="form__slider-value" id="form__slider-value"></div>
<!-- <input type="number" class="form__slider-value" id="form__slider-value" value=""> -->
</div>
<input type="hidden" name="movie_id" id="movie_id" value="<?php echo $post_id; ?>" />
<input type="hidden" name="name" id="name" value="<?php echo $userName; ?>" />
<textarea class="form__textarea" name="remark" id="remark" placeholder="Review"></textarea>
<button type="button" class="form__btn">Send</button>
</form>
and in site it was look like this
This blue mark value I need to update into database. How to do that?
Regarding PHP, you need to handle a form.
Firstly, write a code that would handle the form itself, you can do that, by putting some php on top of your page;
if(isset($_POST['form-slider-value'] && $_POST['form-slider-value'] !== null) {
$value = $_POST['form-slider-value'];
//Database handle
}
Of course don't forget to add name 'form-slider-value' to the input.
Ideally there would be classes or similar stuff for that, I'd advise you to use some kind of framework.
Basically. PHP and FORMS works with handling them, once you submit your form with a button submit, it goes to a place where you address it Form action=. Then everything that is written within names will be at $_POST data.
It is not recommended to keep standalone code in one file, with HTML,css,PHP all together.
Hope that answered your question.
As well. This thing is very common to do or use. I advise you to learn about PHP Forms.
https://www.w3schools.com/php/php_forms.asp
And read about constructing a class. Id say do it the proper way.
Learn about frameworks as well, they are modern day time saviors once you learn it.
CMF MODX revolution
There is a template with a form
<form action="http://localhost/index.php?id=3" method="post">
<input type="name" name="name">
<input type="search" placeholder="Все товары" name="search_bar">
<input type="submit" value="">
<div style="clear: both;"></div>
</form>
which call a snippet which contains debugging line:
echo '|||||||||||| ', $_POST['search_bar'];
The problem.
If I put a value into search bar - echo will not show anything. But if I save the template
before I putt the value into the search bar and click submit button, then echo display right value from the search bar
Suppose the problem is in caching. But I cleaned it and it is cancelled everywhere (at least I think so, exploring administrator panel).
For Snippets that depend on user input (like a search query) you will want to call them uncached: [[!mySnippet]]
However if you want to gain a bit of performance you can wrap the Snippet call with Jason Coward's getCache, and by default it caches to a unique cache key based on the $_GET parameters. In other words, each search query would get its own cache object. This helps when there are repeat searches for the same string.
Having looked at various similar questions, both on SO and elsewhere, I have a horrible feeling what I want to do is impossible, but here goes.
I have a page that is a table of text input rows. The user enters information on each row, and submits the data to a separate file, which creates a PDF.
The problem is that I need the user to be able to add rows to the table at will, since the amount of data can vary.
[Before you go there, I need to point out that I cannot use Javascript for any of this - I know it is easy to do in JS but the page needs to be accessible.]
Here is a very simplified version I just cobbled together to (hopefully) illustrate the point:
<?php
if (filter_has_var(INPUT_POST, 'add_rows')) {
$howmanyrows = filter_input(INPUT_POST, 'howmanyrows', FILTER_SANITIZE_NUMBER_INT);
//get all the data from table and put it in an array,
//then add 5 (or however many) new rows to said array.
}
else if (filter_has_var(INPUT_POST, 'send_data')) {
//get table data, add to session and redirect to other page with a header()
}
?>
<html>
<form action="" method="POST">
<table>
<?php //table rows added using an array of data
foreach ($data as $d): ?>
<tr><td><input type="text" value="<?php echo $d; ?>"></td></tr>
<?php endforeach; ?>
</table>
<input type="text" name="howmanyrows" value="5">
<input type="submit" name="add_rows">
<input type="submit" name="send_data">
</form>
...
</html>
As you can see, at the moment I have a clunky setup where there is just one form that encompasses the entire page, and submits the page to itself. Depending on the button that was clicked, a new row is added or the data is submitted to the PDF-creation page.
This is not ideal, for so many reasons. What I really want to be able to do is have two separate forms, or nested forms. But the former won't allow the input values to be submitted to both, and the latter is apparently bad form (no pun intended) and doesn't work.
Is it at all possible to make this do what I want it to do? Any suggestions for a different way to go about it?
I think you have the best non-javascript solution - certainly hte way I'd run with it.
One thing to make it easier is that you can use multiple inputs with the same name:
<input name="tablerow[]" type="text" value="A" />
<input name="tablerow[]" type="text" value="B" />
<input name="tablerow[]" type="text" value="C" />
And these come through the $_POST['tablerow'] as an array. The length of the array is the number of fields. Then add additional fields to that.
For accessibility, you should add a link at the top that allows the user to hop directly to the first "new" field - otherwise they need to tab through the entire form to get to the new field. (See my comment above about if JS is really unavoidable as you and they can avoid this scenario!)
I'm working on a school assignment, which consists of creating a social network (Basically: Facebook :P). This is in groups, and one of us wrote an auto-complete search engine, which works like this:
http://img585.imageshack.us/img585/333/194d61cbc3fe4cc98005ea1.png
You enter a name, it used some js and php to query the DB to find profiles matching the part of the string you entered. Now, I want to use this functionality to implement the tagging of photos. Now the problem is this: This .js script returns an unordered list of elements, consisting of links (hrefs), and if one is selected:
select: function( event, ui ) {
if (ui.item) {
window.location.href = ui.item.href;
}
}
gets called to navigate to the appropriate profile. Now what I'd like to do is: Enter a string, get the list of query results, and when I click one, i want to load it inside the box. Now I have almost 0 experience with PHP and JavaScript (I had to learn it from scratch basically, I can handle most of it now, but still... :P), and I can't get it to work
Basically that text box is "defined" in the html like this:
<div class="widget" id="search">
<form id="search" method="POST" enctype="multipart/form-data">
<input type="hidden" name="search" value="3559d7accf00360971961ca18989adc0614089c0" />
<div class="field text term "><label for="term">Zoeken</label>
<input type="text" name="term" id="term" class="text" value="" />
</div>
</form>
</div>
How do I access that actual textbox, and put data in it? Any takers? :) I can't seem to get it to work, despite trying almost every name, id or class that I see there. I just need to get the clicked name into the box, so I can just submit it & enter it into the database, as having to enter the entire exact name manually isn't really... Fancy enough
document.getElementById('term').value = newValue;
or, using jQuery (as you specified in the tags):
$('#term').val(newValue);
I seem to have a bit of a problem here that I can't quite figure out.
So the deal is I have a php script along with an HTML file, in that file I have a few forms with some text boxes and some drop downs. Now the reason I had to go single forms on all of these was because if I did one big one none of them would work when I would hit the submit button. I have no idea... My current code is below, my previous code only had one form and it wouldn't work at all.
<form method="post" id="locationFromPost" name="locationFormPost">
<div class="formRow">
<div class="label">
<label for="locationForm">Current Location:</label>
</div>
<div class="field">
//If post is null go back to value pull originally else echo post
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
<input type="text" name="locationForm" id="locationForm" value="<?php echo $location?>"></input>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="sexFromPost" name="sexFormPost">
<div class="formRow">
<div class="label">
<label for="sexForm">Sex</label>
</div>
<div class="dropDown">
<select name="sex" id="sexForm" >
//If post is null go back to value pull originally else echo post
<?php if($_POST['sexForm']==null) $sex=$sex;else $sex=$_POST['sexForm']; ?>
<option value="1" <?php if($sex==1){echo"selected='selected'";}?>>Male</option>
<option value="2" <?php if($sex==2){echo"selected='selected'";}?>>Female</option>
<option value="3" <?php if($sex==3){echo"selected='selected'";}?>>Not Specified</option>
</select>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="submit" name="submit">
<div class="formRow"><div id="seperator"></div></div>
<div class="submitButton">
<input type="submit" name="submit" id="submit" value="Save Changes"/>
</div>
</div>
</form>
So thats what the top part of my code looks like, all the div's are for formatting you don't have to worry about those. Now the php code to save the information is below. I should note that the variables you see in the php parts of the code above are retrieved from the Database in the earlier part of the code, thats working fine. What I am having problems with is I want a user to be able to edit their information then just hit the submit button and the information to be saved. What currently happens is, well, absolutely noting. The page will refresh and all the values will go back to what they were when they were pulled, they are not stored in the DB at all, now I checked to make for sure that part of the code works, buy doing some test. It saves the data no problem. What I thin is the problem is the forms them-selfs, if I do everything in one big form it doesn't work... Don't know why. now if I hit enter at the end of each of the forms individually in the browser the data goes in but just that field, sometimes other fields are wiped completely out and a null or empty string is stored. I cant seem to figure this thing out at all.
heres the php to save the information.
<?php
if (isset($_POST['submit']))
{
mysql_query("UPDATE members SET location= '".$_POST['locationForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
mysql_query("UPDATE members SET location= '".$_POST['sexForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
........ you know the rest.......
}?>
I am completely lost at why this thing is doing this, now im not the best PHP programmer by any-means, so it could (probably is) be me. If anyone could point out what I would need to do to get a system like that working please let me know
Thanks.
I wasn't clear exactly what your problem was.
However this:
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
Doesn't look to me like it is going to do much. Try something like this:
<?php
if(!isset($_POST['locationForm'])){
$location=$location;
}
else {
$location=$_POST['locationForm'];
}
?>
(Curly braces are my own personal liking)
Assuming you have $location defined somewhere you didn't show us. This will check if there is NOT a $_POST['locationForm'], and if there is it will use it.
Try placing all the form fields within on form tag and also use mysql_real_escape_string on your variable as well.
you need all the fields that are going to be submitted to any given page to exist in a single form tag. any elements in a different form tag will submit as a set of THAT form tag.
I' assuming that you also want to maybe combine down your updates?
//$_SESSION['usr'] should already be sanitized and safed wherever you handle your site credentials..
//set your submittable values, with a default to an empty string in this case.
$sex=isset($_POST['sexForm'])?mysql_real_escape_string($_POST['sexForm']):"";
$location=isset($_POST['locationForm'])?mysql_real_escape_string($_POST['locationForm']):"";
//single statement, no need to iterate for each field, that's wasted connections.
$sql="update "UPDATE members SET location= '$location' , sex='$sex' WHERE usr='".$_SESSION['usr']."'";