PHP Submission form into MySQL db formatting - php

I have created a blog using PHP that submits a form into a MySQL databse. It works perfectly. What i want to know is how to get it to recognise when i've pressed enter to create a new line or to record a ' without getting a MySQL error. Basically i want to turn a normal submission form into a submission form like is used on this website.
<tr>
<td>Blog Title:</td>
<td><input type="text" id="edt" name="BlogTitle"></td>
</tr>
<tr>
<td>Blog Content:</td>
<td><textarea name="BlogCont" cols="50" rows="10" id="edt"></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="Name" value="<?=$_SESSION['Name']?>"></td>
<td><input type="submit" id="but" value="Post"></td>
</tr>
Any help would be appreciated.

For new lines with enter use nl2br (manual), for apostrophe you have to escape it with mysql_real_escape_string (manual).

Related

Jquery form multiple fields with Bootstrap

I am using Bootstrap with jQuery and I'm facing issues when passing values from FORM to PHP, in this case the same page.
My code is:
<tr>
<td><input type="text" name="descricao[]" ></td>
<td><span class="spanpreco"><input type="text" name="quantidade[]" ></span></td>
<td><span class="spanpreco"><input type="text" name="preco[]"></span></td>
<td><input type="text" name="subtotal[]" disabled></td>
</tr>
Then my jquery goes like this:
$("#adicionar").click(function(){
$("#linha").append('<tr><td><input type="text" name="descricao[]"></td><td><span class="spanpreco"><input type="text" name="quantidade[]" ></span></td><td><span class="spanpreco"><input type="text" name="preco[]"></span></td><td><input type="text" name="subtotal[]" disabled></td></tr>');
});
The problem here is when I post the data to php, every single column that I dynamically add to the form will not pass to PHP, the result number of rows is always one.
Can someone help me here?
I found the issue, the <FORM></FORM> was on the wrong place.
Thanks and best regards,
Adelmo

PHP Refresh Page to fill HTML Dropdown

I have a HTML Form that is automatically filled with a variable from a SQL table. If I visit the page the dropdown is allways empty and I have to press "F5" once. Then the Dropdown is filled.
Is it possible to put a "Press of the F5 Key" once at the end of my code?
header("Refresh:0"); and other ideas I found online didn't really work.
<tr>
<td align="right">Art der Probe</td>
<td align="right">Type of Test Piece *</td>
<td><select name="inputprobetype"><option value="<?php echo "$row[probe_type]"; ?>"></option><option value="Faser">Faser</option><option value="Matrix">Matrix</option><option value="Halbzeug trocken">Halbzeug trocken</option><option value="Halbzeug ausgehaertet">Halbzeug ausgehaertet</option></select></td>
</tr>
<tr>
<td><input type="submit" name="submit" /><input type="reset" value=" Abbrechen"></td>
</tr>
Thanks for your help.

php display info in form

I have a function allowing members to update their personal details when they have logged in, the update page features a form the user will fill in and when the submit button is pressed the deatils are updated. This works fine, I am wanting to display the users current details in the appropriate text filed in the form e.g. the firstname text field will have value "James" and the user can delete this to enter "jim". Is it something to do with adding a value to the form field? My form is shown below.
<tr>
<td width="78">Firstname</td>
<td width="6">:</td>
<td width="294"><input name="firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td width="78">Surname</td>
<td width="6">:</td>
<td width="294"><input name="surname" type="text" id="surname"></td>
</tr>
Any help is much appreciated
Print value inside the text box.
<input type="text" value="<?php echo $someValue; ?>" />
echo '<input type="text" value="'. $someValue .'" />';
OR
<input type="text" value="<?php echo $someValue; ?>" />
Both obviously require that you be in a .php file, and that $someValue contains the appropriate value to set. Watch out for the double quotes around the value, too. Without them, any spaces will break the value when it's rendered.

Issue using $_POST with a textarea

I have a simple contact form on a website that has 2 text fields, 1 textarea, and 1 hidden field.
For some reason, all the fields POST to a PHP script except the textarea. I have done this a thousand times before and never had this issue.
Here is my HTML:
<form action="scripts/contactform.php" method="post">
<table width="0" border="0" cellspacing="3" cellpadding="5" class="gpass">
<tr>
<td>Name:</td>
<td><input name="name" type="text" maxlength="50" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text"/></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="comment" id="comment" cols="30" rows="5"></textarea>
<input type="hidden" value=" <?php echo $_SERVER['REMOTE_ADDR'];?>" name="address" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" value="Submit" class="noround" id="regbut" /><input name="reset" type="reset" value="Reset" class="noround" id="regbut"/></td>
</tr>
</table>
</form>
And my script looks like this:
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$comment = mysql_real_escape_string($_POST['comment']);
$ipaddress = mysql_real_escape_string($_POST['address']);
I have a few things to process the data underneath this, but that doesn't matter since the $comment variable isn't being defined. I've searched the entire script and there are no conflicting variable names.
I am completely stumped on why this is happening. I've successfully processed textarea's on my site multiple times before, so this really is confusing.
I once experienced an error similar to yours. What helped me was to use different id and name parameters. Try and see for yourself, because you have them identical here.
Though in your case you don't have the textarea set to disabled, the reason I found this post was because I wasn't getting a value from a textarea that was. So here's a note for anyone else with that issue.
To POST the value from a textarea where you want the field to be non-editiable, use readonly instead of disabled - either directly in html or via setAttribute in JavaScript - and then use CSS to highlight it, eg:
textarea[readonly] {background-color:#F0F0F0;})
You only need to add an ID value to the form and then add the form attribute to the textarea with the form ID value
<form id="sample".....>
<textarea name="aba" form="sample".....></textarea>
</form>

How to display error that username is already exists in database on application form on onblur event of username

Suppose I have a application form which contain lot of boxes like email,username,age etc...I have connected this application form to database(mysql) through action="register.php" in application form...
NOw i want that in database,username and email id should be unique.Means no two users cant have same username or email id..
What I know to do This....
When you created tables in mysql then make these fields (username,email) unique(by primary key).Butif i submit the form with same username already in database then error msj will be showed on the next page something like this.username is priamry.....whatever..
Problem in this...
The error msj should be displayed on the onblur event of that textbox(username textbox and email textbox)..
How i can do this.Please explain with example For your ease i am giving code for application form...something lik ethis
applicationform.php
form name="form" action="register.php" method="GET" >
<tr>
<td>Username</td>
<td><input type="text" id="username" size="30" maxlength="35" value=""></td>
<td><div id="p1"></div></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" id="email" size="30" maxlength="30" value=""></td>
</tr>
<tr>
<td align="center" colspan="100"> <input type="submit" name="submit" value="Submit">
</td>
</tr>
I think you have to make an Ajax request when the blur event happens and check at the server side whether the the username exists or not.
This AutoCompleter tutorial should give you the right idea how to do this.

Categories