Localhost error PHP codeigniter 4 local host running error - php

I have complete coding in PHP Codeignitor 4 for inserting data into database, but whenever i try to access localhost its giving error.
i changed my base url too but nothing working...
i have project name ci4_crud then app and subfolders i m using different links like localhost/insert or localhost/ci4_crud/app/views/insert but nothing working ...
in App.php my base url is http://localhost/ci4_crud/
can someone help me plz
<!DOCTYPE html>
<html>
<head>
<title> Registration Form </title>
</head>
<body>
<form method="post" action="<?=base_url() ?> Crud/savedata">
<table border="1" width="600">
<tr>
<input type="text" name="first_name" placeholder="Enter First Name">
</tr>
<tr>
<input type="text" name="last_name" placeholder="Enter Last Name">
</tr>
<tr>
<input type="text" name="address" placeholder="Enter Address">
</tr>
<tr>
<input type="text" name="email" placeholder="Enter Email ID">
</tr>
<tr>
<input type="submit" name="save" value="Save Data">
</tr>
</table>
</body>
</html>

Try putting the following rule in public/.htaccess on line 17:
RewriteBase /ci4_crud

try this
action="<?php echo base_url(); ?>crud/savedata"
or this
action="<?php echo base_url(); ?>index/crud/savedata"
and also may I know what error message you get.

Related

HTML form sending get AND post. Redirecting with 302

When the user clicks submit, it is sending both a post AND get request to the script. The Post returns a 302 Found, and the Get returns a 200, but the print_r shows an empty array after the form is submitted. I can't figure out why this is happening.
This is running on an AWS Linux instance with PHP 5.4 and Apache 2.4.
Here is the code:
<?php
print_r($_REQUEST);
?>
<!doctype html>
<html>
<meta charset="UTF-8" />
<title>Login</title>
<head>
</head>
<body>
<p>Please login:</p>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<table>
<tr>
<td>
username
</td>
<td>
<input type="text" name="username" id="username" style="width:400px;"> (case sensitive)
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<input type="password" name="password" id="password" style="width:400px;">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
When I click submit, this is what I see in the FF dev tools:
Your are not sending POST and GET - only POST.
Your server then responds with a redirect (302) which makes your browser load the given URL via GET. At this point your data is lost (due to the redirect) and the array is empty. The question is why your server instructs to redirect.
The most likely reason is your htaccess file. Could you please send it.

Form submissions within app won't work on Iphone

I'm using the following code to submit a login form:
<div data-role="content">
<form id="test" method="post" action="login.php">
<table>
<tr>
<td>User ID</td>
<td><input type="text" name="userid" size="20" /></td>
<br />
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="20" /></td>
</tr>
</table>
<p>
<input type="submit" name="mysubmit" value="Login" />
</p>
</form>
</div>
This form submission works fine on Firefox, Internet Explorer, Chrome, and Safari on Windows. However, when I'm using Device Preview in Dreamweaver and trying it from my Iphone, I get 'error loading page' on all my form submits. If I put in data-ajax="false", I then get
'{"code":"MethodNotAllowedError","message":"POST is not allowed"}'
when I submit my form. Also, I'm using local server for my sql server.
I even tried using test.php as the action instead of my login.php page with the following blank code for test.php and it still came up 'error loading page':
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
I've tried all this from an android phone also and still get 'error loading page' in Device Preview. This is my first app so once I can figure out this problem, I'll be full steam ahead.
Add ids to your inputs. Instead of
<input type="text" name="userid" size="20" />
Put
<input type="text" id="userid" name="userid" size="20" />

Wamp Server Not Processing PHP properly to Database

As requested by Shankar I am going to post the form code and the php code to this issue. Since yesterday I have moved over to my MAC and installed XAMPP to try to solve the problem but I am still having the same issue. Firstly here is the form code as set in text mate:
<!DOCTYPE HTML>
<head>
</head>
<title>Practising my PHP</title>
<body>
<h2>Subscriber Form</h2>
</body>
<form id="subscriber" action="practice.php" method="post">
<table cellpadding="0" cellspacing="25" border-width="0">
<tr>
<td><label for name="name" id="name" value="">Name:</td>
<td><input type="text" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><label for name="email" id="email" value="">Email:</td>
<td><input type="text" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><input type="reset" id="reset" value="reset"></td>
<td><input type="submit" id="submit" value="submit"</td>
</tr>
</table>
</form>
</html>
Next we have the processing PHP code.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$server = mysql_connect('localhost', 'root', '') or die("The Server Cannot
Be Accessed at This Time");
$db = mysql_select_db('practising') or die("The Database Cannot Be Accessed,
Please Try Again Later");
$query = mysql_query("INSERT INTO `subscribers`(`name`,`email`)
VALUES ('$name','$email')");
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled Out";
} else {
echo "Your Information has Been Added to Our Database";
}
}
?>
I look forward to your comments and help. Oh by the way both files are saved as .php files.
Whatever errors there are in the above can somebody please advise.
What is happening at my end is this. I open the newsletter.php page which displays the form in the browser. When I do anything like fill in one field and test for if statement to notify me that all fields must be filled in nothing happens. I just get the form processing and a bland newsprocess.php page is returned in the processor. Also there is nothing being inserted into the database. I hope that this information is clear enough if you can help me it will be much appreciated. I have also changed my copy of WAMP to 4.0.4. to see if this helps. I also open the file in the urle with the following http://localhost/wamp/TrainingPHP/newsletter.php
Another strange thing is if I us a <?php echo "hello world" ?>as a test outside of this code but on the same page then the echo returns the hello world.
change your mysql_query code
$query = mysql_query($sql, "INSERT INTO subscribers(name,email)
VALUES('$name','$email')") or die(mysql_error());
to
$query = mysql_query("INSERT INTO subscribers(name,email)
VALUES('$name','$email')") or die(mysql_error());
Remove $sql inside mysql_query
Note: Try to use Mysqli or PDO as mysql_* function will be deprecated in future versions of PHP
=========== Edit ===========
change this line <td><input type="submit" id="submit" value="submit"</td>
to
<td><input type="submit" id="submit" name="submit" value="submit"></td>
you have missed ending ">" for input type = submit
give name to the submit button like name="submit" as i have given. as with out name PHP will not recognize this field
you only have given id to the email and name fields you have to give them names. PHP do not recognize ids but it know a field by name. change your both email and name html field like this.
as
<input type="text" id="name" name="name" maxlength="30" maxsize="24">
<input type="text" id="email" name="email" maxlength="30" maxsize="24">
your PHP code is ok but you have issues in your HTML code. you need to have some more practice in HTML and there were some basic mistakes.anyways im posting working code of HTML you can use this and compare it with yours and remove mistakes in you code.
<!DOCTYPE HTML>
<head>
<title>Practising my PHP</title>
</head>
<body>
<h2>Subscriber Form</h2>
<form id="subscriber" action="practice.php" method="post">
<table cellpadding="0" cellspacing="25" border-width="0">
<tr>
<td><label for name="name" id="name" value="">Name:</td>
<td><input type="text" id="name" name="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><label for name="email" id="email" value="">Email:</td>
<td><input type="text" name="email" id="name" maxlength="30" maxsize="24"></td>
</tr>
<tr>
<td><input type="reset" id="reset" value="reset"></td>
<td><input type="submit" id="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

sending data using localhost iis and php

I am having problems sending data using php. Im following some online tutorial for learning php and am having problems. I fairly certain php is running fine on my iis localhost as I ran a test index.php from localhost and everything came back as it should. I also uploaded the files to a web server where I know php is configured correctly. Im guessing that my problem is with syntax.
The text "your name is" and "You live at" is displayed fine but whatever is submitted is not showing up. I'm fairly certain that the problem is user error caused by yours truly. Any suggestions?
Here is the php file that contains the form:
<html>
<head>
<title><?php echo "Form test";?></title>
</head>
<body>
<form action="http://localhost/php/phptest1.php" method="post">
<p>Name:
<input type="text" id="name" size="30 "value="">
</p>
<p>Address:
<input type="text" id="address" size="30" value="">
</p>
</form>
</body>
</html>
And here his the php file to receive the data:
<html>
<head>
<title><?php echo "I have the Info";?></title>
</head>
<body>
<?php
echo "Your name is: ", $_POST['name'], "<br />";
echo "You live at: ", $_POST['address'], "<br />";
?>
</body>
</html>
You have to use the name attribute instead of id:
<input type="text" name="name" size="30" value="">
The reason why your code is not working the way you wanted it to, is that you have 2 errors in your naming convention.
The form fields need to have the name="some_name" added ("some_name" as an example).
Change
<input type="text" id="name" size="30 "value="">
to:
<input type="text" name="name" id="name" size="30 "value="">
and
<input type="text" id="address" size="30" value="">
to:
<input type="text" name="address" id="address" size="30" value="">
Full form code:
<html>
<head>
<title><?php echo "Form test";?></title>
</head>
<body>
<form action="http://localhost/php/phptest1.php" method="post">
<p>Name:
<input type="text" name="name" id="name" size="30 "value="">
</p>
<p>Address:
<input type="text" name="address" id="address" size="30" value="">
</p>
</form>
</body>
</html>
Added note: id can be kept in the OP's code to be used for styling in CSS, which can be kept if needed for styling later down the road.
id="name" and id="address" can safely be removed.

two forms conflict with each other

I have placed two forms on one page. Bothe forms work fine separately but when they are placed on one page at the same time they conflict with each other. Here are both forms:
Contact Form:
<form name="contactform" id="contactform" method="post" action="#targetAnchorPage2">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("contact_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr><td colspan="2"><label id="tworows" for="message" class="<?=$message_class?>">your message:</label></td></tr><tr><td colspan="2"><textarea name="message" cols="22" rows="6" value="<?=$message_class?>"></textarea>
</td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
Subscribe Form:
<form name="subscribeform" id="subscribeform" method="post" action="#targetAnchorPage3">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("subscribe_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
How can this be solved? Is it caused by the "required_once" command?
I am guessing that since you are showing the required files based on the same criteria isset($_POST['name']) and since both forms have the name field you end up showing the code in both requires regardless of which form is submitted. You should simply change the form field names on on of the forms such that they are different.
Both forms have the same action attribute, they both point back to the same page (note that the hash is not sent to the server). As they both have a field called name and you are checking for that, both actions get executed regardless of which form was sent in.
You can do either:
use different scripts / form processors (don't post back to the same page)
use a different check for each form, for example by adding a hidden input that will allow you to distinguish between the forms.
Add
formaction="Your_URL"
Attribut in Button

Categories