MVC block has issue with form not submitting - php

Just some background. I'm using an MVC. My form is put in a div in my template. It posts to the same page where my controller catches it with an isset(), saves stuff to the database, and redirects to a welcome page.
EDIT: It posts everything like it should if I remove the action part of the form. Must be something pertaining to the way index.php reroutes it to ensure that the user has permission to go to that URL, etc. Still looking for a solution.
I've done some debugging and it is apparent that the variables are not showing up in the $_POST variable.
<form method="post" action="<?php echo BASE_URL; ?>/login/register">
<fieldset><legend>Person Info</legend>
<table>
<tr><td><label for="firstname" class="required">Firstname:</label></td>
<td><input name="firstname" id="firstname" type="text" value="<?php echo $firstname; ?>" /></td>
</tr>
<tr><td><label for="lastname" class="required">Lastname:</label></td>
<td><input name="lastname" id="lastname" type="text" value="<?php echo $lastname; ?>" /></td>
</tr>
<tr><td><label for="email">Email:</label></td>
<td><input name="email" id="email" type="text" value="<?php echo $email; ?>" /></td>
</tr>
<tr><td><label for="username" class="required">Username: </label></td>
<td><input name="username" id="username" type="text" value="<?php echo $username; ?>" /></td>
</tr>
<tr><td><label for="password" class="required">Password:</label></td>
<td><input name="password" id="password" type="password" value="<?php echo $password; ?>" /></td>
</tr>
<tr><td><label for="pass2" class="required">Confirm:</label></td>
<td><input name="pass2" id="pass2" type="password" value="<?php echo $pass2; ?>" /></td>
</tr>
</table>
<button type="submit" class="btn btn-custom-lighten">Submit</button>
<button type="button" class="btn"
onClick="parent.location='<?php echo BASE_URL; ?>/nonprofit/welcome'">
Cancel
</button>
<br>
<br>
<button type="button" class="btn btn-primary"
onClick="parent.location='<?php echo BASE_URL; ?>/nonprofits/register'">
Nonprofits Register Form
</button>
<br>
</fieldset>
</form>
You can see my names are consistent from the controller code.
public function register()
{
print_r($_POST);
if (isset($_POST['firstname'])) {
try {
$person = new Person();
$person->handleUpdate($_POST);
$person->setUsername($_POST['username']);
$person->setPassword($_POST['password']);
$person->save();
header('Location: '.BASE_URL.'/login/welcome');
exit();
}
catch (Exception $e) {
$_SESSION['errorMessages'][] = $e;
}
}
$this->template->blocks['content'][] = new Block('registerForm.inc',array('return_url'=>$this->return_url));
}
Any help would be appreciated, but it looks to be an issue with my form. My form does get rendered, it just doesn't submit.

Related

<input> element does not post data?

I have a form page and when I submit this form all inputs are posting successfully except this one:
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()"
<?php if($this->data['kullanici_id']){echo 'readonly';} ?>
value="<?php echo $this->data['kullanici_id']?>">
But why?
-This is my .phtml file:
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" action="/admin/kaydet" method="post" onSubmit="javascript: beforeSubmit();">
<?php if(strlen($this->data['id'])):?>
<input type="hidden" name="id" value="<?php echo $this->data['id']?>">
<?php endif;?>
<font color="green"><h3>DÜZENLE</h3></font>
<img src="/foto/<?php echo $this->data['fotograf']?>" height="110" width="110" align="left" />
<table class="table">
<tr>
<td>T.C. Kimlik No.:</td>
<td><input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>"></td>
</tr>
<?php if(!strlen($this->data['id'])):?>
<tr>
<td>Parola:</td>
<td><input id="password2" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<tr>
<td>Parola Tekrar:</td>
<td><input onchange="passwordCheck(this.value)" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<?php endif; ?>
</table>
<td><button type="submit" class="btn btn-success btn-sm glyphicon glyphicon-floppy-disk">KAYDET</button> </td>
</form>
</body>
</html>
If I have an id; page looks like an edit member page, if I haven't; page will add a new member. In id="TC" input, if I edit a member, this input shouldn't change, so I add a readonly to solve this. But when I submit, input does not post.
Sorry about my bad English :D
Reason your field is not being submitted it is because its set to readonly.
Reason for this is 'If user cannot change the field there is no point of submitting it as value will always remain the same'.
One way to mitigate this behavior is to add hidden field with same name
<input type="hidden" name="kullanici_id" value="<?php echo $this->data['kullanici_id']?>"> />
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>" />

PHP - How can a load page do some of the php

I wish every time I reload or refresh the page. The disabled textbox still disabled. Because my purpose is to modify the textbox once time and then it will be disabled forever. Is this possible?
Below is my code
<tr>
<td><?php echo $lang_txt['leader_id'][$lang]; ?></td>
<td>:</td>
<td><?php if($_POST['txtLeaderID'] == ""){?><input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php }
else
{
?>
<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php
}
?>
</td>
</tr>
Your variant will be worked with
if(!$_POST['txtLeaderID']){echo('<input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')} else {echo('<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')}
For example:
<?php
if (!$_POST['username']){
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
But it didn't work, if we open it in a new tab. You must save this propery anywhere (session, cookie, sql)and check the stored values. it will be work in a new tab.
For example with session:
<?php
session_start();
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = true;
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>

Form validation without losing Form data

I've set up a form with validation in PHP which works fine. The issue I'm facing is that validation occurs once the submit button is clicked. The form values get erased when the submit button is clicked. If there is a validation error, the user needs to fill all the values again instead of just the error one. Is there a way that once submit button is clicked the form values don't get erased?
<tr>
<td><label for="email_address">Email Address:</label> <input type="text" name="email_address" id="email_address" required /></td>
</tr>
<tr>
<td><label for="password">Password:</label> <input type="password" size="25" maxlength="25" name="password" id="password" required /></td>
</tr>
<tr>
<td><label for="password2">Re-enter Password:</label> <input type="password" size="25" maxlength="25" name="password2" id="password2" required /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
<tr>
<?php
if(isset($_POST['submit'])){
$email_address = $_POST['email_address'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email_address)) {
?>
<td><?php echo "Enter Valid Email Address"; ?></td>
<?php
}
elseif ($password2 !== $password) {
?>
<td><?php echo "Passwords don't match"; ?></td>
<?php
}
You need to echo out the user-entry into the value of the input tags. These kind of forms are called "sticky forms"(Since the user input sticks to the form after submitting it).Try doing something like this-
<input type="text" name="email_address" value="<?php echo (isset($_POST['email_address']))?$_POST['email_address']:'';?>" id="email_address" required />
Added this ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This basically echoes out the last entry made by the user into the value field. Try doing this in each of the input tags.

PHP Form not posting all fields

I have a strange issue that I cannot get my head around.
I have the following form and when I click submit I do not GET or POST all fields in the form. Only two fields are being picked up being - isnew=true action=object &submit=Start
<form name="newOffer" action="/auth/dashboard" method="post">
<td><?php echo form_hidden('isnew', 'true');?><?php echo form_hidden('action', 'object');?><input type="text" id="newOfferItem" placeholder="Offer Free Item" class="input-xlarge"></td>
<td><input type="text" id="newOfferText" placeholder="Offer Description" class="input-xlarge" rel="tooltip" title="Description How to get free item"></td>
<td><input type="text" id="newOfferFreeOn" placeholder="Stamps for free item" class="input-xlarge" rel="tooltip" title="Number only. Ex. 5"></td>
<td><span class="label label-danger">Inactive</span></td>
<td><?php $attributes = 'class = "btn btn-success"'; echo form_submit('submit', 'Start', $attributes);?></td>
</form>
You need to add NAME attributes for each INPUT element - either instead of or in addition to the ID attributes.
e.g.
<form name="newOffer" action="/auth/dashboard" method="post">
<td>
<?php echo form_hidden('isnew', 'true');?>
<?php echo form_hidden('action', 'object');?>
<input type="text" NAME="newOfferItem" id="newOfferItem" placeholder="Offer Free Item" class="input-xlarge">
</td>
<td>
<input type="text" NAME="newOfferText" id="newOfferText" placeholder="Offer Description" class="input-xlarge" rel="tooltip" title="Description How to get free item">
</td>
<td>
<input type="text" NAME="newOfferFreeOn" id="newOfferFreeOn" placeholder="Stamps for free item" class="input-xlarge" rel="tooltip" title="Number only. Ex. 5">
</td>
<td>
<span class="label label-danger">Inactive</span>
</td>
<td>
<?php $attributes = 'class = "btn btn-success"'; echo form_submit('submit', 'Start', $attributes);?>
</td>
</form>
Always add a name attribute in input element.
Here is an example for you..
index.html
<form method="POST" action="process.php">
<input type="text" name="username" value="">
<input type="password" name="password" value="">
<input type="submit" name="submit" value="login">
</form>
process.php
<?php
// checking if submit is pressed
if (isset($_POST['submit'])) {
// assigning posted values
$username = $_POST['username'];
$password = $_POST['password'];
// testing
echo $username . "<br>";
echo $password;
}
?>

Show confirmation on referring back to previous page (PHP HTTP REFERER)

This might be simple but I am having difficulty figuring it out,
A form with data in index.php is submitted to sell.php which is processed by mysql query and returns automatically to previous page (index.php) after data is stored in database successfully.
The code I am using is:
header("Location: " .$_SERVER['HTTP_REFERER']);
I needed a little enhancement here. When the page sell.php returns back to index.php, it shall give a confirmation message to user that the data was submitted successfully.
index.php
<form name="vender" method="post" action="sell.php">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
<input type="hidden" name="serial" value="<?php echo $identity; ?>" />
<input type="hidden" name="model" value="<?php echo $model; ?>" />
<input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
<table style="font-size: 8pt;">
<tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
<tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
<tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
<tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
<tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
<tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
sell.php
<?php
include "connect.php";
include "links.php";
$date = $_POST['date'];
$serial = $_POST['serial'];
$model = $_POST['model'];
$imei = $_POST['imei'];
$name = $_POST['name'];
$contact = $_POST['contact'];
$nif = $_POST['nif'];
$qty = $_POST['qty'];
$price = $_POST['price'];
mysql_query("INSERT INTO mobile_sell_data(date,serial,model,imei,name,contact,nif,qty,price) VALUES('$date','$serial','$model','$imei','$name','$contact','$nif','$qty','$price')");
mysql_query("UPDATE mobils SET qty=qty-'$qty' WHERE id = '$serial'");
header("Location: " .$_SERVER['HTTP_REFERER']);
?>
You can't echo anything out once the headers have been sent, since the server is finished dealing with the page when the headers are sent. There are a couple solutions that you can implement here. You could send data back to index using a GET variable, POST variable, SESSION, maybe even a cookie, or you can have the request performed from within the index.php using ajax so that you never actually leave the index page. Here's a simple solution: (Note, you need to remove your redirect in sell.php. Everything takes place in index.php this way)
<?php
$successfulSubmit = FALSE;
if (!empty (#$_POST["sub"]))
{
include "sell.php";
$successfulSubmit = //some logic to verify data was successfully submitted
if ($successfulSubmit)
{
echo "Data submitted successfully";
}
else
{
echo "Data submitted unsuccessfully";
}
}
?>
<form name="vender" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
<input type="hidden" name="serial" value="<?php echo $identity; ?>" />
<input type="hidden" name="model" value="<?php echo $model; ?>" />
<input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
<table style="font-size: 8pt;">
<tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
<tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
<tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
<tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
<tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
<tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
<input type="hidden" name="sub" value="submitted" />
<tr><td><input type="submit" /></td></tr>
</table>
</form>
Have you considered using either a get variable?
header("Location: " .$_SERVER['HTTP_REFERER'] . "?success=true");
Or using a session?
session_start()
$_SESSION['success'] = true
//reset the session on the return page
These aren't particularly elegant solutions, but they will work

Categories