Mix str_replace and regex? - php

I want to replace a string such as:
<input type="hidden" name="id" value="12345" />
but the problem I have is that the values value (12345) is different everytime so how can I do what I'm trying to do? ..I'm guessing regex' but havent a clue

preg_replace('/\<input type="hidden" name="id" value="[0-9]+" \/\>/is', '', $source)

Related

Decoding ampersand (&amp) to normal

I am having problems creating add file form which if person wants, it redirects after submit to add new file, but when I try to do that it still redirects back to m=files because it shows ?m=files&a=addedit. I tried using html_entity_decode, but still it shows the same ?m=files&a=addedit what to do, here is my redirect?
<input type="hidden" name="redirect" value="<?php echo $new = html_entity_decode('m=files&a=addedit'); ?>" />
Not 100% sure what OP wants, but simply replacing &amp with & should solve the problem, I think.
<input type="hidden" name="redirect" value="<?php echo str_replace('&', '&', 'm=files&a=addedit'); ?>" />

PHP: How to post hidden field with style='display:none'

This doesn't get posted if it's hidden like this, how do i get around it?
<input name="Pid" style="display:none"/>
and PHP.
$Pid = preg_replace ('#[^0-9 ]#i', '', $_POST['Pid']);
Kindly use it as:
<input name="Pid" type="hidden" />
You can use hidden field simply setting type as hidden
<input type="hidden" value="" name="pid"/>
If you really need your input type to remain, like text or checkbox, use a <div style='display:none;'></div> tag to hide your input object.
Sometimes, this is required if your submit code does not include checks like
if (isset($_POST['Your_Object_name'])) {...}
Sometimes you don't want to show them, but neither do you want your code to issue error messages.
Your code will then be as follows
<div style='display:none;'>
<input name="Pid" type="text" />
</div>
Et voila

handle html input elements with numeric id javascript

i have html form
<form name="updatefrm" id="updatefrm" action="" method="post">
<input type="hidden" name="98" id="98" value="" />
<input type="hidden" name="99" id="99" value="" />
<input type="hidden" name="100" id="100" value="" />
<input type="hidden" name="101" id="101" value="" />
<input type="hidden" name="102" id="102" value="" />
<input type="hidden" name="updateqty" id="updateqty" value="1" />
</form>
now i want to assign value to this elements
i m using following javascript code for element with id 98
document.updatefrm."98".value = elements[0].value;
but its giving me error in console.
can any one help me with this ?
You should use document.formname to access forms as not all browsers support this(actually I'm not sure if any does). use document.forms.formname instead. Also to access a numeric property of an object use bracket notation instead of dot notation.
document.forms['updatefrm']['98'].value = elements[0].value;
Why can't I have a numeric value as the ID of an element?
// Change your numeric id.
var myValue=document.getElementById('your_changed_id').value;
alert(myValue)
Go for simplicity..Instead of writing this complex code.why not try a simple code which does the same thing
document.getElementById('your_changed_id').value
You can also use getElementById to set the value. Beside this also check Javascript Naming Convention
document.getElementById('98').value = "YOUR VALUE";
You should not give numbers as ID include any kind of character..
Try like this:
document.forms["updatefrm"]["s98"].value = elements[0].value;
Fiddle

reading two form elements with same name

<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same

PHP Request Form - I am not sure how to use it

I am hoping that there is someone that can help me with this question. I am a ASP programmer and not sure how this works in PHP
echo '</textarea>
<input type="hidden" name="g_word" id="g_word" value="$_POST[g_word]" />
<input type="hidden" name="article_no" id="article_no" value="$_POST[article_no]" />
</form>';
How do I use the $_POST[article_no] in the example above? In asp I would have used it like this "+Request.Form("article_no")+". How would I do it in PHP?
Thanks
if you use the solution posted above, please add some basic protection against xss injection - for example htmlentities($_POST['article_no'])
echo '</textarea><input type="hidden"
name="g_word" id="g_word"
value="'.$_POST[g_word].'" /> <input
type="hidden" name="article_no"
id="article_no"
value="'.$_POST[article_no].'" /></form>';
Close the single quote, and use a dot to concatonate
$value = "cool";
echo 'My String is ' . $value . '!!!!!';
In this case, the dot is the same as the plus concatenation operator.
echo '</textarea><input type="hidden"
name="g_word" id="g_word"
value="'.$_POST['g_word'].'" /> <input
type="hidden" name="article_no"
id="article_no"
value="'.$_POST['article_no'].'" /></form>';
You have to put article_no between '-s.
I think I've understood your question; feel free to let me know if not.
In PHP (and many other languages), the number of quotes around a string determines how the string is parsed. If single quotes are used, then nothing in the string is parsed (except for another single quote — it will need to be escaped with a backslash if you intend it to be a part of the string rather than the closequote). If double-quotes are used, more things are parsed, but you accordingly have to do more escaping.
There are a variety of ways of dealing with inserting variables in strings.
Using double quotes:
echo "</textarea><input type=\"hidden\"
name=\"g_word\" id=\"g_word\"
value=\"$_POST['g_word']\" /> <input
type=\"hidden\" name=\"article_no\"
id=\"article_no\"
value=\"$_POST['article_no']\" /></form>';
Using single quotes:
echo '</textarea><input type="hidden"
name="g_word" id="g_word"
value="' . $_POST['g_word'] . '" /> <input
type="hidden" name="article_no"
id="article_no"
value="' . $_POST['article_no'] . " /></form>';
Or, in my opinion the most elegant way, using (s)printf to return a formatted string:
printf('</textarea><input type="hidden"
name="g_word" id="g_word"
value="%s" /> <input
type="hidden" name="article_no"
id="article_no"
value="%d" /></form>', $_POST['g_word'], $_POST['article_no']);
Variables aren't interpreted inside of single quotes. However, they are inside double quoted strings, or heredoc. Personally, I'd switch out of PHP mode entirely, like so:
<?php
//...
?>
</textarea><input type="hidden"
name="g_word" id="g_word"
value="<?php echo htmlentities($_POST['g_word']); ?>" /> <input
type="hidden" name="article_no"
id="article_no"
value="<?php echo htmlentities($_POST['article_no']); ?>" /></form>
<?php
//...
This is even more readable if you do some formatting and use short tags -- although, it requires a non-default configuration option, and there are other disadvantages, primarily if you have XML docs parsed by the PHP interpereter, or your app is going to be installed on servers you don't control.
That'd look like this:
<form>
<textarea>
<?
//...
?>
</textarea>
<input type="hidden" name="g_word" id="g_word" value="<?= htmlentities($_POST['g_word']); ?>" />
<input type="hidden" name="article_no" id="article_no value="<?= htmlentities($_POST['article_no']); ?>"/>
</form>
<?
//...

Categories