Displaying text after space in PHP - php

I have my website connected to a database with MySQL and I am echoing out the data but it doesn't display information after a space. E.g. in the database 'Hello there' would just be 'Hello'.
I know this is quite a common question asked but I just couldn't get mine working. An example is this:
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>

You're missing quotes around your HTML attribute value. Without it the first word is considered the value and everything else is considered a new HTML attribute.
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
Should be:
County: <input type = "text" name = "county" value = "<?php echo $row["county"]; ?>"> <br><br>
//^ ^

Related

Unable to delete mysql data from php

I have successfully connected to database and published mysql data on web and manually added joke on it.Screenshot of what output looks like is provided here.
When i manually enter the corresponding id of joke inside the textbox and press delete button, it deletes the joke corresponding to that it. But when i click on the delete button directly, that joke isn't deleted. How to update value of that textbox ? For the time being, i have made input type of textbox as text so to see whats going inside it. I am using PHP version: 7.1.1 and Apache/2.4.25 (Win32.
<p>Add your own joke</p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke) : ?>
<form action ="?deletejoke" method='post'>
<blockquote>
<p><?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8');?>
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
<input type = 'submit' value = 'Delete'>
</p>
</blockquote>
</form>
Screenshot of Mysql database is provided here.
there is a space between <? and php on the input value it should be <?php;
then it should work.
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
<input type="text" name="id" value="<?php echo $joke['id'];?> ">
you should remove the spaces (not just in this line)
Change
<input type = "text" name = "id" value = "<? php echo $joke['id']; ?>">
to
<input type = "text" name = "id" value = "<?php echo $joke['id']; ?>">

Sign In won't Work

I have created a basic sign in form:
<form action = "" type = "post">
<input type = "text" name = "txt_user" placeholder = "Username" required/>
<input type = "password" name = "txt_pass" placeholder = "Password" required/>
<input type = "submit" name = "btn_submit" value = " Sign In ">
and posted this above my html:
<?php
if(isset($_POST['btn_submit'])) {
$username = $_POST['txt_user'];
echo $username;
?>
to see if the form works, it doesn't. The txt_user won't display up top the html as it should be but instead change my url from this:
localhost:8080/tryouts
to this:
http://localhost:8080/tryout/?si_username=asd&si_password=asdasd&si_submit=++Sign+In++
can you tell me what's going on? I tried this method before and it works. Maybe I misplaced something?
Update
The page containing the form is called signin.php where it's been placed inside index.php like this:
<body>
<?php include("signin.php"); ?>
</body>
purpose of this is to save time and space for the php codes so that I won't be bother changing the sign in form from page to page, instead just changed the signin page main php page. I don't think it's causing this but still...
You need to set your form method to POST; form type is invalid. Right now, it's defaulting to using GET.
<form action = "" method = "post">
<input type = "text" name = "txt_user" placeholder = "Username" required/>
<input type = "password" name = "txt_pass" placeholder = "Password" required/>
<input type = "submit" name = "btn_submit" value = " Sign In ">

HTML repopulate input text or textarea with original text

I want to have a text-field (input type="text") or text-area in html that takes users input. After "submit" clicked, PHP returns results. I want to repopulate the text-field or text-area with original user input. However my code only works with text-field, but not text-area:
This works:
<INPUT TYPE = "text" NAME = "seqbox" SIZE = 50 PLACEHOLDER = "Enter sequence here" VALUE = "<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>">
This does not work:
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here" VALUE = "<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>"></TEXTAREA>
Any idea why? Thanks!
Textarea doesn't have a value property. You need to set it in between the element like this:
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here">
<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>
</TEXTAREA>
Textareas don't take a value attribute.
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here"><?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?></TEXTAREA>
A note: you should run $_GET['seqbox'] through htmlspecialchars or malicious users will be able to inject things like JavaScript into your page through a specially crafted URL (an XSS vulnerability).

Query execution php script in the same page

I have this form html code in update.php. For updating, it's required to link to another page save_seeker.php with mysql update script for it to be executed. Is there any way to execute the script in same page on submitting form so that after query execution it remains on same page?
<form action= "save_seeker.php" method = "post">
Update details !<br><br>
First Name
<input type = "text" name = "fname" value = "<?php echo $disp['fname'];?>"><br><br>
Last Name
<input type = "text" name = "lname" value = "<?php echo $disp['lname'];?>"><br><br>
Contact number
<input type = "text" name = "contact" value = "<?php echo $disp['contact'];?>"><br><br>
Email-id
<input type = "email" name = "email" value = "<?php echo $disp['email'];?>"><br><br>
Address
<input type = "text" name = "address" value = "<?php echo $disp['address'];?>"><br><br>
Experience
<input type = "number" name = "experience" value = "<?php echo $disp['experience'];?> "><br><br>
Qualification
<input type = "text" name = "qualification" value = "<?php echo $disp['qualification'];?>"><br><br>
<input type = "Submit" value = "Update">
</form>
You could do the processing in the same file by adding this into the form action.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<!-- Input fields in here-->
<input type="submit" name="form_submit" value="Submit">
</form>
Now check for the submit action by checking if it is set in the post variable
<?php
if ( isset( $_POST['form_submit'] ) ) {
// Do processing here.
}
?>
You could use the include('page-with-update.php') , call the update function and use the $_SERVER["PHP_SELF"].
I hope helped!!

Undefined variable: POST in C:\wamp\www\forms.php

The following code doesn't work. forms.php and forms.html are the file names, both saved in the root directory.
The error is "Undefined variable: POST in C:\wamp\www\forms.php". I know I'm not handling multiple inputs properly, either. Can anyone help with either of these?
<html>
<head>
<title>
Registration for Placement
</title>
</head>
<body>
<h1> Start your Placement process...NOW...</h1>
<form action = "forms.php" method = "POST">
<p> Name <input name = "Name" type = "Text" size = "20" maxlength = "30">
<p> Branch: CSE<input name = "branch" type = "radio" >
ECE<input name = "branch" type = "radio">
MEC<input name = "branch" type = "radio">
<p> Languages Known:English<input name ="lang[]" type = "checkbox" value = "1">
Hindi<input name = "lang[]" type = "checkbox" value = "2">
Tamil<input name = "lang[]" type = "checkbox" value = "3">
<p> State<select name = "state" size = "2">
<option> Jammu and Kashmir
<option> Delhi
<option> Tamil Nadu
<option> M.P
<option> U.P
<option> Maharashtra
</select>
<p> Thanks for submimitting the form
<p><input type = "Submit" value = "enter" >
<input type = "Reset" value = "clear" >
</form>
</body>
</html>
<html>
<head>
<title>Thank You</title>
</head>
<body>
<h1>Thank You</h1>
<p>Thank you for registering. Here is the information you submitted:</p>
<p>Name </p><p><?php echo $POST['Name']; ?></p>
<p>Branch </p><p><?php echo $POST["branch"];?></p>
<p>Lang </p><p><?php echo $POST["lang[0]"];?></p>
<p>STATE </p><p><?php echo $POST["state"];?></p>
</body>
</html>
Instead of $POST, use $_POST, more info:
http://php.net/manual/en/reserved.variables.post.php
you should write like this
$_POST['Name'] or
$_REQUEST['Name']
Don't know much but maybe the "double quotes" are creating a problem. Try using
$_POST['state'];
This might do the trick.
Its $_POST not $POST also you should be checking the variable is defined and set, also you should use htmlspecialchars() on user input when outputting to save you from a XSS vulnerability:
<?php
echo isset($POST['Name']) ? '<p>'.htmlspecialchars($_POST['Name']).'</p>' : null;
?>

Categories