Hei. I would very much appreciate some help with this. I am creating a very simple "find and replace" application to generate links. I am using the code below to do this.
It now replaces the word "chocolate" with anything I'd like. But i would also like to change other words within the same text with the push of the same button.
Forexample; if i would like to change the word "loves" to "hates" as well.
How would i do this? Appreciate all the help i can get.
<?php
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$search_length=strlen($search);
if(!empty($text)&&!empty($search)&&!empty($replace))
{
$strpos=strpos($text,$search);
$text=substr_replace($text,$replace,$strpos,$search_length);
echo $new = str_replace(' ', '%20', $text);
}
else
{
echo 'pls fill in all fields';
}
}
?>
<form action='index.php' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake</textarea><br><br>
<input type='hidden' value= 'chocolate' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
</form>
You can use arrays in str_replace, so this code will work using 2 comma separated valus in the input field:
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=explode(",",$_POST['searchfor']);
$replace=explode(",",$_POST['replacewith']);
var_dump($replace);
$text=str_replace($search,$replace,$text);
echo $text;
}
<form action='#' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake </textarea><br><br>
<input type='hidden' value= 'chocolate,cake' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
If you want to use 2 (or more) input fields use this code:
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=explode(",",$_POST['searchfor']);
$replace=array($_POST['replacewith'],$_POST['replacewith2']);
var_dump($replace);
$text=str_replace($search,$replace,$text);
echo $text;
}
<form action='#' method='POST'>
<textarea name='text' style="display:none;" rows='6' cols='30'>http://andy.com/loves/chocolate/cake </textarea><br><br>
<input type='hidden' value= 'chocolate,cake' name='searchfor'><br><br>
Replace the word chocolate with:</br>
<input type='text' name='replacewith'><br>
Replace the word cake with:</br>
<input type='text' name='replacewith2'><br><br>
<input type='submit' value='find and replace'>
Related
This code generates multiple form associated with each product and its id.
But at html side only first is working. When I inspected the page I came to know that this code only generates form for first product only. Anyone Else faces this?
for ( $b = 0; $b < sizeof( $id ); $b++ ) {
echo "
<form action='Post.php' method='GET'>
<div class='form-group' style='display:none' id='$id[$b]'>
<label class='control-label'>Message</label>
<input type='text' name='id' value='$id[$b]'style='display:none'>
<input type='text' name='nam' value='admin 'style='display:none'>
<textarea type='text' class='form-control ' rows='4' col='10' name='mess' >
</textarea>
<input style='margin-top:10px' type='submit' class='btn btn-info' value='Submit'>
</div>
</from>";
}
You seem to close your form tag with 'from'.
change /from to /form
I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example
Ex. (HTML file)
<form name='test' method='POST'>
<input type='text' name='name'>
<input type='text' name='surname'>
<input type='submit' name='sub'>
</form>
Now in the PHP file I'll get the $_POST like:
$_POST['name']= something
$_POST['surname']= something
etc...
What about if I want to "group" that to made a $_POST like this:
$_POST[name-of-the-form]['name']= something
$_POST[name-of-the-form]['surname']= something
etc...
How could I do it?
AbraCadaver is spot on here, all you do is name your form element accordingly.
e.g.
<input type='text' name='name-of-the-form[name]'>
Assuming you are doing this from a PHP file, you should use square braces andn the same name of in all element names:
<?php
$formName = 'test';
?>
<form name='<?=$formName?>' method='POST'>
<input type='text' name='<?=$formName?>[name]'>
<input type='text' name='<?=$formName?>[surname]'>
<input type='submit' name='sub'>
</form>
so I have a form. The form consists of 10 lines by default. It goes like this:
<form method="post" action="actionhere">
<?php
for($i=0; $i<10;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='submit' name='submit' value='Submit'/>
</form>
So inside the form, we will have 10 rows of checkbox+textbox.
What I'm trying to make is, I want to place a button to add new row (the checkbox+textbox). Now, problem is, I need the $i value (since it's form the for loop). Is that possible that when we click the add row button, the value of $i that we set inside for loop be incremented by 1 on each click? I know we can clone the div using jquery, but how about the $i value?
I think you are doing it in wrong way you do not need $i value inside name attribute you have to use array for it for example
<form method="post" action="test.php">
<div class='clone_me'>
<span>Line 1</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
<span>Line 2</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
</div>
<input type='submit' name='submit' value='Submit'/>
</form>
Now implement this code in actionhere.php
<?php
$cks = $_POST['ck'];
$txs = $_POST['tx'];
foreach($cks as $key => $ck) {
echo $ck."<br>";
echo $txs[$key]."<br>";
}
?>
Well, basically no. Your PHP script is already over when the html has been generated. So you can't rely anymore on PHP. But you don't need to make it explicitly appear in your html.
You should count the rows using jquery :
var i = $('form').find('.clone_me').length
and then add a new row using javascript again :
$('form .clone_me:last').clone().insertAfter('form .clone_me:last');
<input type='hidden' name='counter' id='counter'/>
<input type='checkbox' name='chk' id='chk'/>
<?php
$counter=$_POST['counter'];
for($i=0;$i<=$counter;$i++)
{
$chk=$_POST['chk'.$i];
// Your Insert Code Here
}
?>
may be this can be but have some limit
if you have no problem with page reload the you can do it is:-
by this way your page will reload and the value in textbox and checkbox will gone:---:)
every time new page generate and send by server to client browser
<?php
if(isset($_POST['submit'])){
$ends = $_POST['ttl_rows'];
}else{
$ends = 10;
}
?>
<form method="post" action="#">
<?php
for($i=1 ; $i<$ends;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='hidden' name='ttl_rows' value='<?php echo ($i+1); ?>'/>
<input type='submit' name='submit' value='Submit'/>
</form>
I have a html form for uploading a file, which is as follows:
$uploadhtml = htmlspecialchars(json_encode("<form action='up.php' method='post'
enctype='multipart/form-data'>
<label for='file'>Filename:</label>
<input type='file' name='file' id='file'/>
<br />
<input type='hidden' name='pk' value='".$pk."'>
<input type='hidden' name='username' value='".$USERNAME."'>
<input type='submit' name='submit' value='Submit' onclick= />
</form>"), ENT_QUOTES);
I would like to know if it is possible to call the setTimeout function to update a particular layer, like follows:
onclick="setTimeout('updateLayer("text", "ff", "ok"))',1250);"
updateLayer takes 3 variables as arguments, how would I specify them as parameters within quotes?
Something like this:
onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"
You can also backslash the quotes. Note that this only works with " qoutes and not ' quotes in php, but works with both quotes in javascript:
onclick="setTimeout(function() { updateLayer(\"text\", \"your's\", \"ok\"); } ),1250);"