HTML embedded PHP note executed when statement is true - php

I'm working on a basic webform, using php to dislpay error messages and process the data input by the user.
I also use it to echo and ID for html elements to change their color on error:
<h3 class="question" <?php if ($error['field1'] === 1)
{echo('id="red"');}?>Fill in field 1</h3>
<p class="error"><php echo $errormessage['field1'];?></p>
<input name="field1" type="text" value="<?php echo $formfields['field1'];?>"
maxlength="128" <?php if ($error['field1'] === 1){echo ('id="error_field"');?>>
I have a class handling the form and all the errors. $error['field1'] becomes 1 if field1 is empty, and an error message is displayed by $errormessage['field1'].
The problem is, in half of the form the php embedded to echo the ID's (red and error_field) works. All the input and <h3> become red, as defined in CSS for the ID's.
But halfway trough the form, only the $errormessage and $formfields still work. The other php code doesnt work anymore. It seems to not execute anymore but if as I check all the parameters it should be executing.
Any idea why php is not executing half of the code?

I cannot understand your problem since I have not access to full source of your file but at the moment I found a lot of syntax errors... pay attention! I have no idea what you are going to do but you can begin fixing your syntax...
<h3 class="question" <?php if ($error['field1'] === 1) { echo('id="red"'); } ?>>Fill in field 1</h3>
<p class="error"><?php echo $errormessage['field1']; ?></p>
<input name="field1" type="text" value="<?php echo $formfields['field1']; ?>" maxlength="128" <?php if ($error['field1'] === 1) { echo('id="error_field"'); } ?>>

Related

Foreach with 2D (Multi-Dimensional) input names, how to grab the data & update database?

I have looked everywhere so I apologize if there's an answer to this already, I couldn't find it after hours of searching. Maybe it's not a good way to do it either...
I have a form that has "picks" of a litter, so 1st pick, 2nd pick, 3rd pick, etc. These picks also have genders (Female, Male)
I use a foreach query to grab the available pick spots. If someone isn't assigned to that pick spot, then the input field is enabled for manual typing
Within that foreach query, I need to somehow submit what pick it is and what gender, as well as the typed value, so I can find the pick and store it to the database... The other problem is, it's dynamic where someone may type in 3 input fields, or other times just 1 input field. Basically it'll show 3 input fields and the user can choose to use all 3 or just 1...
No matter what I've tried, it doesn't work. I don't even have my examples because I've tried everything I could find and had to scrap it all
The input, this is for males only but females are the same:
<?php
foreach ($malespickedquery as $pick){
$picktaken=$pick['user_id'];
$pickcustom=$pick['custom_name'];
$pickposition=$pick['pick_id'];
$suffix=ordinal($pickposition);
?>
<div class="form-row pb-3">
<div class="col-12">
<label class="control-label"><?php echo $pickposition.$suffix." Pick "; ?>Male:</label>
<?php if($pickcustom==true){ ?>
<input type="text" name="" class="form-control" value="<?php echo $pickcustom; ?>" />
<?php }elseif($picktaken!="0"){ ?>
<input type="text" name="" class="form-control" value="<?php echo $pick['public_name']; ?>" disabled />
<?php }else{ ?>
<input type="text" name="malepicks[<?php echo $pickposition; ?>][male][]" class="form-control" value="" />
<?php } ?>
</div>
</div>
<?php } ?>
I've tried foreach within foreach, but can never get all information. Most things just return the last input field as well... So if 3 of them are generated, the last input field would come through but not the others. I also did something with foreach where it returned ALL of the fields, even blank ones, which just got confusing and still didn't work right
Thank you in advance!

String display incorrectly when in codeigniter input value using the form helper

I have a strange issue I never had before.
I have a record stored in my database, it is a dutch place name as follow:
's-Heer Abtskerke
if I use the form helper:
<div class="form-group">
<?php
echo form_label('Plaats','plaats');
echo form_error('plaats');
echo form_input('plaats',set_value('plaats',$object->plaats),'class="form-control" id="plaats"');
?>
</div>
I am getting this output:
's-Heer Abtskerke
And if I use the html input element:
<input type="text" name="plaats" class="form-control" id="plaats" value="<?php echo $object->plaats; ?>">
I get the correct output:
's-Heer Abtskerke
I am wondering what is going on in here!
You need to turn off html escape:
set_value('plaats','\'s-Heer Abtskerke', FALSE)
Here you have doc

confused about basic find and replace app

I am trying to do a find and replace applicaiton the problem is that after cliked submit button all the text fields gets clean nothing displays on the screen What am i doing wrong
<?php
$offset=0;
if(isset($_POST['text'] ) && isset($_POST['searchfor']) && isset($_POST['replacewith'])){
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$searchLength=strlen($search);
if(!empty($text) && !empty($search) &&!empty($replace)){
while ($strpos= strpos($text,$search,$offset)){
echo $offset=$strpos+$searchLength;
}
} else {
echo "<script>alert('errorrrr')</script>";
}
}
?>
<form action="#" method="post">
<textarea name="text" id="" cols="30" rows="10"></textarea><br>
Search For:<br>
<input type="text" name="searchfor"><br>
ReplaceWith<br>
<input type="text"name="replacewith"><br>
<input type="submit" value="Fr..."></>
</form>
Regarding your form, you decided to submit to the same page.
Doing this, the page is obviously fully reloaded when submitted. Hence it is normal that what you typed in has disappeared.
If you want to see it again, you have to display you variables in the HTML code.
For example:
<?php
$myVar = "";
if(isset($_POST['myVar']){
$myVar = $_POST['myVar'];
}
?>
<form>
<input type="text" value="<?php echo $myVar;?>"/>
</form>
NB: I encourage you to filter the user entry.
Regards
there is problems in your code :
1 - echo $offset=$strpos+$searchLength; the echo can't be used in this format. insted use echo $offset; in next line for seeing offset values.
2 - if the text be like 'amir love persepolis' and search for 'amir' to replace it with 'all men's' you will have another issue, because you will have while ( 0 ) situation. think about this too!

if isset breaking my php

I'm trying to build a SQL statement using variables that were posted on a previous page (show the SQL statement for copy-pasting in a div using echo), however, the one statement is preventing me from doing it.
<?php
if (isset($_POST['locationName'])) {
echo $_POST['locationName'];
}
?>
As soon as I've put that in, the page refuses to load. It doesn't make any sense to me as I've got similar code throughout the page for the other input fields (to repopulate them after form submission).
As an exmaple: this works perfectly:
<input type="checkbox" name="lead" value="1" <?php if(isset($_POST['lead'])) echo "checked='checked'"; ?> />Lead Required<br>
I've been trying to change the quote types, and tried using htmlspecialchars as well as htmlentities in the echo, but each time I leave the in statement in, it breaks the page.
As soon as I comment that if statement out, the page loads again.
Here's the input field for the $_POST['locationName'] value:
<input required autofocus type="text" pattern="[a-zA-Z0-9-'\s\!\#\*]+" name="locationName" maxlength="100" size="67" placeholder="Place Name" value="<?php echo isset($_POST['locationName']) ? $_POST['locationName'] : '' ?>" />
<input type="checkbox" name="lead" value="1" <?php if(isset($_POST['lead'])){echo "checked='checked'";} ?> />Lead Required<br>
try to use braces and check

CodeIgniter highlight fields with errors on form submission

I could use some guidance in how to proceed on form validation in CodeIgniter. I am using CodeIgniter's built-in form validation and works fine as far as it goes. It returns individual error messages for each field where there is an error by using and wrapping it in some HTML/CSS for styling:
<?php echo validation_errors('<p class="error">'); ?>
But what we want to do is highlight the fields where there are errors. CI will let you put the error messages next to where the form errors are. But it requires you to use the error message for the value, like this:
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
which by the way is in CI's manual in non-CI syntax, which puzzles me. Anyway, we also want the data from the field when the form is submitted to be preserved and returns. So I've done:
$email = array('name' => 'email', 'value' => $em);
?><div style="padding:5px;">Email* </div><?php echo form_input($email) . '<br/>';
$em is returned from the controller like this:
$data['em'] = $this->input->post('email');
So my question is, how do I accomplish all of what is outlined above? Obviously, what CI suggests and what I have done collide. But I don't know how else to do it, so I could use some help.
EDIT: Upon further digging, I see that you can put the error message next to the field by doing this:
<?php echo form_error('email'); ?>
But I'm not getting any message upon an error, even though I have the rule written and I get an error with the first line of code above.
form error($field) returns an empty string '' so better use:
<input type="text" name="email" <?php if (form_error($email) !=='') { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
Tested.
In order to display error individually you should use the function form_error('email'). And for you to get a value of a field being checked, use the function set_value('email'). For these two functions to work, you would have had to, in your controller, set a rule for the 'email' field. Where you specify wich validation rules apply to that field.
<?php echo form_error('email'); ?>
<input type="text" name="username" value="<?php echo set_value('email'); ?>" size="50" />
source: http://codeigniter.com/user_guide/libraries/form_validation.html#individualerrors
untested, but form_error($field) should return true if there is an error:
So perhaps:
<input type="text" name="email" <?php if (form_error($email)) { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
untested. worth a shot?
Perhaps also consider using JQuery or similiar to validate and style the form fields, and use CI as a fallback (for presentation purposes, obviously).
That way your form validation can be styled as you required without CI limitations for 99% of validation rules, and then anything else, the default CI way can kick in.

Categories