I'm new to this PHP thing. Trying to escape some php script in a form and can't figure out how to do it.
I've tried all different combinations of \' and \" but I'm stuck. Please the relevant portion of script and advise what I'm doing wrong.
This is the example of input:
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
And this is the rest of the display/redisplay part of the script so you can see it in context...
//Redisplay/Display form
$self = htmlentities($_SERVER['PHP_SELF']);
$displayOutput .= '
<form action="' . $self . '" method="post">
<fieldset>
<div>
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
</div>
<div>
<label for="email"> Email :</label>
<input type="email" name="email" id="email" />
</div>
<div>
<label for="mailFormat">Mail Format</label>
<select name="mailFormat" id="mailFormat">
<option value="plain">Plain text</option>
<option value="html">HTML</option>
</select>
</div>
<div>
<input type="checkbox" name="terms" id="terms" value="yes" />
<label for="terms">Tick this box to confirm you have read our terms and conditions</label>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</fieldset>
</form>';//close form
try to change this line of code
<input type="text" name="fullName" id="fullName" value=\' "<?php echo $C_fullName ?>" \' />
to
<input type="text" name="fullName" id="fullName" value="'.$C_fullName.'" />
and apply this change at other places
The problem is that you are trying to escape into PHP evaluation from within PHP. That doesn't make sense.
When a document is parsed, it is interpreted as normal, verbatim text until the interpreter hits an escape sequence like <?php. Everything until the closing ?> (or till the end of file) is interpreted as PHP. That's why putting another <?php opening sequence into your code is nonsensical.
The form markup in above code is build as a PHP string. What you are trying to do is to create a string and concatenate a variable into it. PHP offers multiple ways to do this. The simplest, in your case, is to use the concatenation operator .. Close the string, concatenate the variable, and concatenate the rest of the string.
… value="' . $C_fullName . '" …
It's no different than the action definition in the first line.
You might also want to look at the heredoc syntax, it can be quite useful for situations like yours.
You can give a try with the following code :
$displayOutput .= '
<form action="' . $self . '" method="post">
<fieldset>
<div>
<label for="fullName">Full Name : </label>
<input type="text" name="fullName" id="fullName" value='; echo $C_fullName; $displayOutput .=' />
</div>
<div>
<label for="email"> Email :</label>
<input type="email" name="email" id="email" />
</div>
<div>
<label for="mailFormat">Mail Format</label>
<select name="mailFormat" id="mailFormat">
<option value="plain">Plain text</option>
<option value="html">HTML</option>
</select>
</div>
<div>
<input type="checkbox" name="terms" id="terms" value="yes" />
<label for="terms">Tick this box to confirm you have read our terms and conditions</label>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</fieldset>
</form>';
Related
I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>
I have the following form
<form action="/wp-content/themes/wallstreet/welcome.php" method="post" class="basic-grey">
<h1>Form
<span>Please fill all the texts in the fields.</span>
</h1>
<label>
<span>Your Nickname* :</span>
<input id="name" type="text" name="name" placeholder="It will appear " />
</label>
<label>
<span>Your Email* :</span>
<input id="email" type="email" name="email" placeholder="Valid Email Address" />
</label>
<label>
<span>Message* :</span>
<textarea id="messagebook" name="messagebook" placeholder="The text that will appear "></textarea>
</label>
<label>
<span>Code* :</span>
<input id="code" type="text" name="email" placeholder="The Code That we sent to your email" />
</label>
<label>
<span> </span>
<input type="submit" class="button" value="Send" />
</label>
As you can see, it works with a PHP which submits the form to a txt file.
<?php
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
exit();
?>
Well, my intention is to store a TXT file which will store 1000 unique codes, like
AD23A ASSD5 FJLQQ AS223 AS544 ... (Etcetera)
The txt format will be the same as above.
Well so, i want to do something with this pice of code
<label>
<span> </span>
<input type="submit" class="button" value="Send" />
</label>
so the submit button only should work when the code in the field coincides with a code in my .txt file.
Forgive the title, this is the best way I can think to word my issue.
Basically, I have a html form inside some php code, but I need to include javascript functions (with parameters specified) inside the form. But I have an issue with the single and double quote marks escping the php code. See below:
echo'<form id="create_booking_form" onsubmit="return false;">
<div>Student Name: </div>
<input id="students_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('students_name')" maxlength="30">
<div>Parents Name: </div>
<input id="parents_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_name')" maxlength="30">
<div>Parents Email Address: </div>
<input id="parents_email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_email')" maxlength="100">
<div>Booking Time: </div>
<input id="booking_time" type="text" onfocus="emptyElement('status')" onkeyup="restrict('booking_time')" maxlength="8">
<div>Comments / Questions: (max 255 characters)</div>
<input id="comments" type="text" onfocus="emptyElement('status')" onkeyup="restrict('comments')" maxlength="255">
<div>Password: </div>
<input id="password" type="password" maxlength="16">
<br /><br />
<button id="createbookingbtn" onclick="createbooking()">Submit your booking</button>
<span id="status"></span>
</form>
</div>
</div>';?>
My issue is for example: onkeyup="restrict('students_name')"
If i use single quote to specify 'student_name' it escapes the echo command form the php code. If i use double quotes it escapes the restrict() function...
What can I do?
Much appreciated
Is anyone able to help me out here?
If your string is created with ''s, you can print a ' character by escaping it: \', and if it's created with "'s, then you can print it by escaping it to \".
In your situation, however, I wouldn't do either. I'd just end the PHP tag and start it again:
?>
<form id="create_booking_form" onsubmit="return false;">
<div>Student Name: </div>
<input id="students_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('students_name')" maxlength="30">
<div>Parents Name: </div>
<input id="parents_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_name')" maxlength="30">
<div>Parents Email Address: </div>
<input id="parents_email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_email')" maxlength="100">
<div>Booking Time: </div>
<input id="booking_time" type="text" onfocus="emptyElement('status')" onkeyup="restrict('booking_time')" maxlength="8">
<div>Comments / Questions: (max 255 characters)</div>
<input id="comments" type="text" onfocus="emptyElement('status')" onkeyup="restrict('comments')" maxlength="255">
<div>Password: </div>
<input id="password" type="password" maxlength="16">
<br /><br />
<button id="createbookingbtn" onclick="createbooking()">Submit your booking</button>
<span id="status"></span>
</form>
</div>
</div>
<?php
But what when I want the HTML content to be in a variable?
Just use an output buffer.
ob_start();
?>
<p>Some HTML</p>
<?php
$html = ob_get_clean();
I would place the javascript in an external file, and have the php place the link to that file instead.
Put a \ in front of the quote to escape:
echo'<form id="create_booking_form" onsubmit="return false;">
<div>Student Name: </div>
<input id="students_name" type="text" onfocus="emptyElement(\'status\')" onkeyup="restrict(\'students_name\')" maxlength="30">
etc
You can escape your quotes by adding a single backslash before them..
How may I get data of this form to be received in an email?
<form class="pa">
<fieldset>
<label>Name <em>*</em></label>
<input type="text" placeholder="">
<label>Email <em>*</em></label>
<input type="text" placeholder="">
<label>Age <em>*</em></label>
<input type="text" placeholder="">
<label>Phone <em>*</em></label>
<input type="text" placeholder="">
<label>Zip Code <em>*</em></label>
<input type="text" placeholder="">
<a class="submit pa" href="#"><img src="wp-content/themes/child/img/submit.png"</a>
</fieldset>
</form>
You'll need a server-side script to accept the form data, and send an email with it. You can do this PHP and several other languages. Judging by your sample code you have WordPress, so you might look into the ContactForm plugin.
wrap your input's with something like
<form class="pa" action="yourscript.php" method="post">
.
.
<label for="name">Name <em>*</em></label>
<input type="text" placeholder="" id="name" name="name">
.
<input type="submit" value="Submit" />
<!-- remove the a tag because that won't submit your form data or use type="image" -->
<input type="image" src="wp-content/themes/child/img/submit.png" />
</form>
then use $_POST['name'] to retrieve the value
you have to add name='' to each inputs to have them sent to your script eg <input type="text" id="name" name="name"> and they have to be different otherwise they might get overridden.
also your labels will need the for= to be useable which uses the id of the input eg <label for="name">Name <em>*</em></label>
I hope someone can help me. I'm a little at loss on how I can achive this: I'm trying to pass infos generated in my php to another form made in Javascript. For example, if I put my first name in the input field and click submit, then it would go to another page with the actual form and have the first name already filled there.
One thing that I did notice was that the javascript form isn't within the <form> tag, it's in a bunch of tables with some input fields. I can post what it looks like in pastebin if this can help in understanding what I mean.
Also with this script im unable to edit it, I did not make it, it is one of those script you just place on your site thats auto generated.
My form looks like this:
<form action="auto-form/index.php" method="post" name="openleads" onsubmit="return checkForm(this);">
<label for="first">First Name <span class="required">*</span></label>
<input id="first_name" type="text" name="first" applicationforms="true" /><br>
<label class="error" for="name" id="first_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Zip <span class="required">*</span></span>
<input id="zip" type="text" name="zip" applicationforms="true" /><br>
<label class="error" for="name" id="zip_error" style="color:#F00; font-size:11px;">This field is required.</label>
<label for="last">Last Name <span class="required">*</span></label>
<input id="last_name" type="text" name="last" applicationforms="true" /><br>
<label class="error" for="name" id="last_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Email <span class="required">*</span></span>
<input id="email" type="text" name="email" applicationforms="true" /><br>
<label class="error" for="name" id="email_error" style="color:#F00; font-size:11px;">This field is required.</label>
<input class="button" type="submit" name="send" value="Send" />
</form>
Any help is appreciated; like I said, I'm a bit at loss on what to do with this one.
php-form.php
<form action="javascript-form.php" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
javascript-form.php
<form action="" method="">
<input type="text" name="name" value="<?= (isset($_POST['name'])?htmlentities($_POST['name'],ENT_QUOTES):''); ?>" />
<input type="submit" value="Submit" />
</form>
Use PHP to output the POSTed values in to the value attribute of the form fields. You can also use GET variables and use javascript to parse the window.location and scrape those form values.
this seemed to get this done
<script type="text/javascript" src="http://jquery.offput.ca/js/jquery.timers.js"></script>
<script>
$(document).everyTime(1000,function(i){
if($('#ui-datepicker-div').length>0)
{
$('#first_name').val('<?php echo $_POST['first_name']; ?>');
$('#last_name').val('<?php echo $_POST['last']; ?>');
$('#zip').val('<?php echo $_POST['zip']; ?>');
$('#email').val('<?php echo $_POST['email']; ?>');
}
})
$('#first_name').val('test');
</script>