Using javascript function inside html form being echoed inside php - php

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..

Related

PHP form including an extra quotation mark in the variable name

I have a slightly strange error (shown below)
This is the code in my login.php file
<form action="loginScript.php" method="post">
<input class="center" type="text" name="username" placeholder="Username" required>
<br>
<input class="center" type="password" name="password" placeholder="Password" required>
<br>
<input class="center" type="submit" value="Login">
</form>
My loginScript.php file uses var_dump($_POST).
Remove name="username" and retype it. Did you copy and paste the code from somewhere? Probably your editor does not show hidden special characters.

PHP: Can't escape characters in for inputs

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>';

How to send details to a php page?

How do I send information from a HTML page to a php page which then makes a connection to a database?
This is my code so far on my website: http://jsfiddle.net/xiiJaMiiE/wNraL/
<h1>
<div id=signin>
<input type="text" name="textbox1" value="Username" onfocus="if
(this.value==this.defaultValue) this.value='';" size="19%" height="50"/></br>
<input type="password" name="password" value="Password" onfocus="if
(this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
</div>
</h1>
Thanks in advance!
Replace your h1 element (you don't have a heading so you shouldn't say you do) with a form element.
Give it an action attribute that specifies the URL of the PHP program you want to submit the data to.
Since you are sending authentication data, add a method attribute that to POST the data.
<form action="signin.php" method="POST">
<div id=signin>
<label>
Username
<input type="text" name="username">
</label>
<label>
Password
<input type="password" name="password">
</label>
</div>
</form>
The data will then be available to PHP via $_POST['field_name_here'].
<div id=signin>
<form action="connection.php" method="POST">
<input type="text" name="username" value="Username" onfocus="if (this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
<br>
<input type="password" name="password" value="Password" onfocus="if (this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
</form>
</div>
You can access these passed variables in the conneciton.php file with $_POST['username'] and $_POST['password'].
you should set the action attribute to a file that will process form data (eg process. php) and pass form data to your database using $_POST('filedName') on submitting the form.

Passing php form info to javascript form

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>

How can I make custom Wordpress comments form?

I have made following form, but it doesn't work because it doesn't send post id in post request.
<?php
require('./wp-blog-header.php');
$post = get_post($_GET['p']);
?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" >
<label>Name : </label><br/>
<input name="author" id="author" type="text"/><br/>
<label>Comment : </label><br/>
<textarea name="comment" id="comment"></textarea><br/><br/>
<input name="submit"type="submit" id="submit" value="Submit" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</form>
Wordpress will drop any url parameters that it does not recognize. One way to add custom parameters in a url string is to use Add_Query_Args() function.
Take a look at Add_Query_Args Function Reference
This should solve your issue. Good luck.
With only a bit of tweaking was it possible to make it work.
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<p><input type="text" name="author" id="author" value="" size="22" tabindex="1" />
<label for="author"><small>*</small></label></p>
<p><input type="text" name="email" id="email" value="" size="22" tabindex="2" />
<label for="email"><small>*</small></label></p>
<p><textarea name="comment" id="comment" cols="48" rows="10" tabindex="4" onFocus="clearText(this)" onBlur="clearText(this)" ></textarea></p>
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</form>
Above code works (for me). Basically, you were missing an id on the form. WP apparently uses that id as part of the validation process.
So, to make it work, add id="commentform" to your form-tag, and it should work.

Categories