I have the following input field :
<input data-type="customerName" value="<?php echo isset($invoice['Client']['customerName']) ? $invoice['Client']['customerName']: ''; ?>" type="text" class="form-control" id="clientCompanyName" placeholder="Company Name">
When the user types in it, it utilizes autocomplete to pull data from my sql database and display names, and the user picks the name that is pulled from the database.
When the form is saved, the name, and the uuid is saved to the form. I have another code tidbit :
<a target="_blank" href="add-client.php?uuid=<?php echo isset($invoice['Client']['customeruuid']) ? $invoice['Client']['customeruuid']: ''; ?>">
This links to another page to update client information, but it only works after the form is saved.
I am trying to make the link work all the time, immediately after the user selects a customerName. Is there a way to do this?
Yes, you need to use jquery and ajax, php code is run on the server when the page loads, it is not dynamically applied while the page is open. You need to use javascript or jquery to handle user input without reloading the page. If this user input needs to query a database, you will have to use a seperate php page, and access that information using $.ajax with jquery.
I would strongly suggest you to go for the AngularJS way. You can use expressions to dynamically format your link.
Check this: http://www.w3schools.com/angular/angular_expressions.asp
<div ng-app="" ng-init="myName='Foo'">
<input type="text" ng-model="myName">
<a ng-href="http:\\www.{{myName}}.com">{{myName}}</a>
Substitute 'Foo' with your retrieval from the database and you may be good to go.
If you wanna test how it works, I did a JSFiddle: https://jsfiddle.net/ny39728p/
Related
I have a html/php page, which reads file NAMES on the server, collects user inputs, and returns results upon user clicking "submit" button. The returned results will be parsed and displayed in a table in the page. Certain functions are implemented in javascript, btw.
That's how the html/php usually works. Now I need another field that will allow user to specify a name, and I need "refresh" the page with additional info which will be read from file NAMES on the server. Let me call this ShowExtraName function, for the sake of wording.
Before ShowExtraName, I have html/php code like this (sorry for the format, not sure how to write html tags in the post):
<html>
<head>
<javascripts added here>
<table>
<div>
<form>
<continue html layout here>
<checkboxes with php reading file NAMES from server --- line XXYY>
<submit>
<php script to check user input and query server, then display returns in table. This section is blank before submit since certain input fields are not "isset".>
Now my question is how/where I can add the ShowExtraName. I believe it needs to be php, because I need determine not only user-specified name, but also get additional and related information from the server and display it in line XXYY.
Hope I describe it clearly. Thank you for your input!
You have to use JavaScript/jQuery and AJAX calls in order to be able to ask the server to execute some code and then to render the result back to the HTML without refreshing the page.
jQuery is a very simple and yet very powerful tool.
You can do this a number of ways.
Using only html and php, the easiest would probably be to use a form that uses GET with an action of the same page. Then check for get parameters in your PHP and print the extra row of data in your code.
<FORM action="" method="get">
<INPUT type="text" id="name"><BR>
<INPUT type="submit" value="Send">
</FORM>
<?php
if (isset($_GET['name'])) {
//do database query
//echo more stuff
}
?>
If you are doing a database query be sure to use prepared statements
Otherwise if you want to get fancy with a live refresh you will need to use JavaScript. (jQuery AJAX)
Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.
The problem I'm having is this:
I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.
I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.
It's a little complicated, so I'll use a simple example.
Let's say I have a basic table in a MySQL database with three columns: ID, Name,
and EmailAddress.
Here's the query that grabs the data:
$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.
Now let's say I have a simple form with two inputs and a submit button.
HTML
<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>
Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.
When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.
I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.
Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form
Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.
You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.
I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.
I'm making an administrative page for a website, for editing front-end contents. I don't want to use JavaScript, I want to make it with raw PHP.
The thing is: The user will choose a page from the select box, and the contents according to the page will be loaded into two text boxes. Then the user will edit them on text boxes and update the contents using an update button.
(there is no submit button for the select box, I want 'onselect' data content load)
I need, the user will simply select a page, no button to be pressed then and the content will be loaded. I want not to deploy JavaScript for this. Is it possible using raw PHP? Any suggestion?
PHP is a server side programming language. You can't really affect the client side directly, only indirectly through forms etc.
If you accept your users may have to use the enter key, which will submit the form, you're fine.
But you can't have an action occurring on a select, without even using the enter key, without javascript.
Note that the requirement to not use javascript, especially for an administrative tool, doesn't make sense.
Yes, it's possible with raw PHP, but I do not recommend it.
You can put the select-box inside a
<form action="targetFile.php" method="GET">
To submit the form without a submit-button, you can use:
<select name="sel" onChange="this.form.submit()">
When the form is submitted, the browser loads the site "targetFile.php"
On this site, you can use php to set the text-box content:
<input type="text" value="
<?php
if($_GET['sel'] == 0) echo "content1";
else echo "content2";
?>
">
If you want to load the content dynamically (without realoading a site) you have to use Javascript and maybe Ajax.
I have a PHP web page. Initially, the values in the text box are retrieved from a LDAP directory based on the user login information. The user makes any required changes in the values and on clicking submit the values in the text box get updated and also the values in the Directory get updated as well( I have got this part done). I am new to PHP and I am trying to figure out how to update the values in the text box on submit.
With PHP you can only do changes on submit when you do a AJAX call to the server. However, you can do changes using only PHP after submitting it on the server side.
Updating in the browser directly "onsubmit" is not possible without the help of JavaScript.
You want to take the values the user has submitted and redisplay them in your text field.
Something like
<input type="text" name="phone_num" id="phone_num" value="<?php (isset($_POST['phone_num']) ? $_POST['phone_num'] : $valueFromLDAPDir) ?>" />
I am working on a messaging system for a networking website wherein I want to use the data that the user enters. Like if the user enters 'Andy' as the recipient I want the php mechanism to use the $_POST['recipient'] variable to retrieve the id of that user and conduct a lookup rather than just going into a javascript function and terminating. I want the javascript function which actually sends the message to get called after the user clicks another button. I am new to PHP and don't understand the inner workings that well. I have given a small sample code below that is similar to mine but easier on the eyes.
<form>
To:<input type="text" id=rec">
<input type="submit" value="confirm" id="confirm" name="confirm">
<button type="button" id="sub1" onclick="sendPM()">Submit</button>
</form>
<--some mysql look ups using $_POST['rec'] which copies values from the table into the
php variables and stores it in hidden HTML fields to be accessed later by the
sendPM() function-->
$('#sub1').click(function sendPM(){.......}
If you are familiar with JQuery, you may want to use the JQuery .post() method to send a request to another page on your site. Basically, move all of your php to a file called chat.php, and use the JQuery to hit that page and return the results. This would also allow you to do all of the processing without having your user have to refresh the page.